加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 了解linq语法

发布时间:2020-12-15 07:48:40 所属栏目:百科 来源:网络整理
导读:我真的很难理解它的内部工作 这是LINQ语法 string[] test = new test[] { "abc","","cd","aa" };test = test.Where(x = !string.IsNullOrEmpty(x)).ToArray(); 我对于语法如何管理感到困惑.它把所有数组放在x中吗?如果是,那么它如何管理x空值? 要么 如果不
我真的很难理解它的内部工作
这是LINQ语法
string[] test = new test[] { "abc","","cd","aa" };
test = test.Where(x => !string.IsNullOrEmpty(x)).ToArray();

我对于语法如何管理感到困惑.它把所有数组放在x中吗?如果是,那么它如何管理x空值?

要么

如果不是,那么测试数组的值会一个一个的放在x中?

解决方法

你应该考虑其余的答案,他们是非常准确的,我想要告诉你,也许这将帮助你理解的语法是这种查询实际上可以用这样的查询语法来表示:
string[] test=new test[]{"abc","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测试指示源IEnumerable<>在这种情况下迭代 – 你的数组
>哪里有很好的解释,它只是从你的数组中选择所有的非空字符串
>最后的选择实际上是数据的投影.

所有这一切都等同于你的代码

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

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

基本上,羔羊是代表的捷径,所以你在做什么:

x => !string.IsNullOrEmpty(x)

您正在创建一个匿名方法,并将该方法分配给delegate参数. x表示字符串变量.

这个话题真的很广泛,试图在这里解释一下,但我希望这给你一个了解背后的想法.

Btw你可以结合语法如下:

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

如果你的谷歌LINQ就像是谷歌幽默,网络是困扰着LINQ的文章,样品等.

一个好的起点是来自微软的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;
   }
}

正如你可以看到委托被称为函数(委托是指向函数的指针)
屁股什么功能???你在代码中声明的那个

> x => !string.IsNullOrEmpty(x),x表示从Where方法传回给外部代码的参数,您可以在其中检查并使用它来过滤结果
> x =>是声明一个委托的缩写
>!string.IsNullOrEmpty(x)这是匿名方法的正文,你可以看到它符合Func< T,bool>的要求.它返回一个bool(谓词过滤数组的元素),作为一个参数,它接收到通用T,在这种情况下,它是一个字符串从你的数组

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

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

使用这种语法很容易显示它们实际上是方法(匿名方法)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读