Can Linq and lambdas be used without including the System.Linq namespace?

Some time ago I was working on a rather twisted project - I could write code in only one area, and later it will be added to the C # function (by another module).

I could only use namespaces that were previously declared (I did not affect them) and I could only use variables from the scope in which I worked. Because of this, I was unable to change the headers and libraries.

The problem arose when I wanted to use shared collections - I could not use either lambda expressions or LINQ - I just was not able to deliver using System.Linq; since I did not have access to the file header.

I only had to do a simple thing, and without LINQ or lambda it was easy to manage. However, after that I was wondering what would happen if I had to use some more complex operations on IEnumerable. This begs my question:

Is it possible to use LINQ or lambdas without changing the file header and adding new namespaces?

Say a List<int> _Numbers = new List<int>(); . Let it be filled with some numbers. Now I want to select all even numbers from it.

With using System.Linq; in the title, 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 including LINQ, how can I achieve this? My first guess would be something like:

 List<int> _NewList = System.Linq. // ??? What to add here? 

I know that the problem is quite exotic, and this is very unusual for this, but I'm just curious, and I did not find information about this case.

+6
source share
3 answers

Without the using directive, methods such as .Where , .Select will not be allowed. This means that LINQ part of the language will not work. You will have to encode it long (and back!):

 List<int> _NewList = System.Linq.Enumerable.ToList( System.Linq.Enumerable.Where(_Numbers, _Number => _Number % 2 == 0) ); 

Note that this is becoming increasingly complex for longer examples with groups, orders, let, joins, etc. If you are not doing something fairly simple, it would be wise to just find a way to add the using directive.

Note that if _Numbers is a List<T> , you can simply use (for this single example):

 List<int> _NewList = _Numbers.FindAll(_Number => _Number % 2 == 0); 
+15
source

You can write code like this:

 System.Linq.Enumerable.Where(_NewList, n => n % 2 == 0) 

But be careful: if your class implements the IQueryable<T> interface, the code above will do the job, but more slowly.

 System.Linq.Queryable.Where(_NewList, n => n % 2 == 0) 

Because IQueryable is a way to create C # in another DSL, like SQL. In other where conditions, your SQL server can select the items you need as quickly as possible. But if you work with IEnumerable or System.Linq.Enumerable , it will not be optimized on the server side, you will get a complete list of elements, all of them will be loaded into memory and filtered in memory.

+3
source

It is not clear why you cannot access the header file, but if there is a problem that you cannot enter at the beginning of the file using dirrective, you can still use the fully qualified type name, for example:

 System.Linq.Enumerable(...) 

To make this work, you naturally must include the correct links.

If this is not what you are asking for, please specify.

+2
source

Source: https://habr.com/ru/post/949449/


All Articles