Some time ago, I've been working on a quite twisted project - I could only write the code in the single scope and it would be later put in a C# function (by another module).
I could only use namespaces which were declared before (I had no influence on them) and could only use variables from the scope I worked in. Because of that, I wasn't able to change the headers and included libraries.
The problem came when I wanted to operate on generic collections - I couldn't use neither lambda expressions, nor LINQ - I simply wasn't
able to put using System.Linq;
, as I had no access to the file header.
I had to do only a simple thing and it was easy to manage without LINQ or lambda. However, after that I was wondering what would happen, if I had to use some more complex operations on IEnumerable. From that, there comes my question:
Is it possible to use LINQ or lambdas without changing the file header and adding new namespaces?
Let's say that we have a List<int> _Numbers = new List<int>();
. Let it be filled with some numbers.
Now I want to select all the even numbers from it.
With using System.Linq;
in the header, the solution is obvious:
List<int> _NewList = _Numbers.Where(n => n % 2 == 0).ToList();
or
List<int> _NewList = (from _Number in _Numbers where _Number % 2 == 0 select _Number).ToList();
But without LINQ included, how can I achieve this? My first guess would be something like:
List<int> _NewList = System.Linq. // ??? What to add here?
I am aware that the problem is rather exotic, and it's very unusual to go this way, but I'm just curious and I've found no information about this case.
Without the using
directive, methods like .Where
, .Select
, will not resolve. That means that LINQ the language part will not work. You would have to code it long-hand (and backwards!):
List<int> _NewList = System.Linq.Enumerable.ToList(
System.Linq.Enumerable.Where(_Numbers, _Number => _Number % 2 == 0)
);
Note that this becomes increasing complex for longer examples with groups, orders, "let"s, joins etc. Unless you are doing something fairly simple, it would be advisable to just find a way to add the using
directive.
Note, however, that if _Numbers
is a List<T>
, you can just use (for this single example):
List<int> _NewList = _Numbers.FindAll(_Number => _Number % 2 == 0);
You can write such code:
System.Linq.Enumerable.Where(_NewList, n => n % 2 == 0)
But, be careful: if your class implements interface IQueryable<T>
, code above will do the work, but more slower.
System.Linq.Queryable.Where(_NewList, n => n % 2 == 0)
Because IQueryable
is the way to produce C# in other DSL language, like SQL. With other where
conditions your SQL server can choose needed elements as fast as it can. But if you working with IEnumerable
or System.Linq.Enumerable
there no be optimise on server-side, you'll got a full list of items, they all will be loaded to the memory and filtered in memory.
Not very clear why you can not access file "header", but if the issue that you can not type in the begining of the file using
dirrective, you can still use full qualified name of the type, like:
System.Linq.Enumerable(...)
To make this work, you naturally, need to include also correct references.
If this is not what you're asking for, please clarify.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With