c# - 了解 linq 语法

标签 c# .net linq syntax

<分区>

我真的很困惑理解它的内部工作 这是 LINQ 语法

string[] test = new test[] { "abc", "", "cd", "", "aa" };
test = test.Where(x => !string.IsNullOrEmpty(x)).ToArray();

我对 where 语法如何管理感到困惑。是否将所有数组都放在 x 中?如果是那么它如何管理 x 空值?

如果不是则测试数组值一一放入 x 中?

最佳答案

您应该考虑其余的答案,它们非常准确,我想向您展示的可能会帮助您理解语法的是,这种查询实际上可以用如下查询语法表示:

string[] test=new test[]{"abc","","cd","","aa"};

// this is the equivalent to your code
// added explicit type to make it clearer, it's optional
var a = from (string)x in test
        where !string.IsNullOrEmpty(x)
        select x;

如果您熟悉 SQL,您会发现这种语法更容易阅读,即使您不知道它,这种语法也更清晰。

编译代码时,此查询语法会自动转换为 C# 方法语法,以生成 IL,因此如果您分解 DLL,您将看到方法语法而不是查询语法

关于这段代码的简要说明:

  • 如你所见x变量已声明,类型为 string .为什么?因为您的数组是一个字符串数组

  • in test注明出处IEnumerable<>迭代 - 在这种情况下是您的数组

  • where很好解释,它只是从数组中选择所有非空字符串

  • 最后是实际上是数据投影的选择。

所有这些都等同于你的代码

现在您可能会问自己……我应该在什么时候使用一种语法或另一种语法?嗯,它们是等价的,但查询语法运算符是有限的,这意味着大多数操作都是使用方法语法而不是查询语法完成的。我一直在做的是尝试编写更易于阅读的代码,如果使用查询语法编写代码,则某些代码更易于理解。

关于方法语法,x => ...语法被称为 lambda 表达式,如果您是第一次使用它们,它们可能看起来很奇怪,但您最终会爱上它们。

基本上 lambda 是委托(delegate)的快捷方式,所以你正在做什么:

x => !string.IsNullOrEmpty(x)

您正在创建一个匿名方法并将该方法分配给委托(delegate)参数。 x代表 string variable .

这个话题真的很广泛,无法在这里解释,但我希望这能让您了解背后的内容。

顺便说一下,您可以像这样组合语法:

// this is the equivalent to your code
// added explicit type to make it clearer, it's optional
var a = (from (string)x in test
        where !string.IsNullOrEmpty(x)
        select x).ToArray();

如果你用 google LINQ 就像用 googling porm 哈哈,网络上充斥着 LINQ 文章、示例等。

Microsoft 的 101 个示例是一个很好的起点

http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

编辑

我将尝试模拟 Where 方法,以便您可以获得更好的 lambda 表达式示例

// this is basically the declaration of one overload of the Where method
// the this in the parameter declaration, indicates this is an extension method which will be available to all IEnumerable<> objects
// the Func<T, bool> is the interesting part, it is basically a delegate (as a reminder, the last parameter of the Func object indicates the type that must be returned, in this case is a bool)
// the delegate is simply a pointer to a function 
public IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{

   ...some logic




   ... yielding back the reuslts
   foreach(var r in res)
   {
      // here is the delegate in action
       if(predicate(r))
           yield return r;
   }
}

如您所见,委托(delegate)是作为函数调用的(委托(delegate)是指向函数的指针) 屁股什么功能???您在代码中声明的那个

  • x => !string.IsNullOrEmpty(x)x指示从 Where 方法传回外部代码的参数,您可以在其中检查它并使用它来过滤结果

  • x =>是声明委托(delegate)的缩写

  • !string.IsNullOrEmpty(x) 这是匿名方法的主体,如您所见,它满足 Func<T, bool> 的要求它返回一个 bool 值(用于过滤数组元素的谓词)并且作为参数,它接收到通用的 T。在这种情况下,它是您数组中的一个字符串

另一种声明 lambda 表达式的方法是:

test = test.Where(
            (string) x =>
            {
                return !string.IsNullOrEmpty(x)
            })
           .ToArray();

用这种语法很容易表明它们实际上是方法(匿名方法)

关于c# - 了解 linq 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10846488/

相关文章:

c# - 将 void 传递给泛型类

c# - EF 4.3 代码优先 : Table per Type (TPT) with Composite Primary Key and Foreign Key

.net - 编译时数组边界

c# - C# 类向后兼容性规则/避免重大更改

c# - EF 一对多 where 条件

c# - 在 SharePoint(自定义操作)或 C# 项目中获取货币汇率的最佳和最简单方法是什么

.net - 在运行时编译可移植类库

c# - 为什么这个 linq 查询返回一个 bool 值而不是选择的第一个结果?

c# - 如何在字典集合中使用 LINQ 将 TimeSpans 添加到一起

c# - RDLC报表中心如何显示报表数据?