是否可以在不包含System.Linq命名空间的情况下使用Linq和lambdas? [英] Is it possible to use Linq and lambdas without including a System.Linq namespace?

查看:72
本文介绍了是否可以在不包含System.Linq命名空间的情况下使用Linq和lambdas?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前段时间,我一直在从事一个非常扭曲的项目-我只能在单个作用域中编写代码,然后将其放入C#函数中(由另一个模块).

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.

当我想对通用集合进行操作时出现了问题-我既不能使用lambda表达式也不能使用LINQ-我根本就没有 能够放入using System.Linq;,因为我无权访问文件头.

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.

我只需要做一件简单的事情,而且无需LINQ或lambda即可轻松进行管理.但是,在那之后,我想知道会发生什么,如果我不得不使用一些 IEnumerable上更复杂的操作. 随之而来的是我的问题:

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:

是否可以在不更改文件头和添加新命名空间的情况下使用LINQ或lambda?

假设我们有一个List<int> _Numbers = new List<int>();.让它充满一些数字. 现在,我要从中选择所有偶数.

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.

标题中有using System.Linq;,解决方案很明显:

With using System.Linq; in the header, the solution is obvious:

List<int> _NewList = _Numbers.Where(n => n % 2 == 0).ToList();

List<int> _NewList = (from _Number in _Numbers where _Number % 2 == 0 select _Number).ToList();

但是不包括LINQ,如何实现呢? 我的第一个猜测是:

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.

推荐答案

如果没有using指令,则无法解析.Where.Select之类的方法.这意味着LINQ 语言部分将不起作用.您将需要长期编写代码(并且要反向编码!):

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)
);

请注意,对于带有组,订单,"let",联接等的较长示例,这变得越来越复杂.除非您做的非常简单,否则建议仅找到一种添加using指令的方法.

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.

但是请注意,如果_NumbersList<T>,则可以使用(对于此示例):

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);

这篇关于是否可以在不包含System.Linq命名空间的情况下使用Linq和lambdas?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆