Is it possible to use Linq and lambdas without including a System.Linq namespace?

后端 未结 3 657
一整个雨季
一整个雨季 2020-12-11 04:57

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)

3条回答
  •  情书的邮戳
    2020-12-11 05:23

    You can write such code:

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

    But, be careful: if your class implements interface IQueryable, 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.

提交回复
热议问题