LINQ:点表示法VS查询防爆pression [英] LINQ: Dot Notation vs Query Expression

查看:139
本文介绍了LINQ:点表示法VS查询防爆pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用LINQ一般(到目前为止并toxml用于toSQL)。我已经看到了,有时有两个或两个以上的方法来达到同样的效果。就拿这个简单的例子,据我了解都返回同样的事情:

I am beginning to use LINQ in general (so far toXML and toSQL). I've seen that sometimes there are two or more ways to achieve the same results. Take this simple example, as far as I understand both return exactly the same thing:

SomeDataContext dc = new SomeDataContext();

var queue = from q in dc.SomeTable
        where q.SomeDate <= DateTime.Now && q.Locked != true
        orderby (q.Priority, q.TimeCreated)
        select q;

var queue2 = dc.SomeTable
        .Where( q => q.SomeDate <= DateTime.Now && q.Locked != true )
        .OrderBy(q => q.Priority)
        .ThenBy(q => q.TimeCreated);

此外,我可能已在语法或缺少参数或不同的任何错误,这个想法是,有两种方式可以EX preSS同样的事情;据我所知,第一种方法有一定的局限性,而且点号是比较完整的,但除此之外,还有什么其他的优势?

Besides any mistake I may have made in the syntax or a missing parameter or difference, the idea is that there are two ways to express the same thing; I understand that the first method has some limitations and that the "dot notation" is more complete, but besides that, are there any other advantages?

推荐答案

点记号通常被称为lambda语法。第一个符号的推移,一些名字,但我通常把它叫做查询语法。

The "dot" notation is usually called Lambda syntax. The first notation goes by a number of names but I usually call it the query syntax.

我的工作在一个团队中的10名开发人员,我们认为,在长约我们应该作为标准使用。在一般情况下,更调味(与LINQ)开发移向拉姆达语法,但也有例外情况显著

I work on a team of 10 developers and we argue at length about which we should use as a standard. In general, the more seasoned (with LINQ) developers migrate towards the Lambda syntax but there are significant exceptions.

LAMBDA更简洁,但执行多表连接是一场噩梦。加入只是更干净的查询语法。另一面是,有许多的LINQ操作,只有内拉姆达语法存在:单(),第一(),次数()等

Lambda is more concise but performing multiple table joins is a nightmare. Joins are just much cleaner with the query syntax. The flip side is that there are a number of LINQ operations that only exist within the Lambda syntax: Single(), First(), Count() etc.

那么,用什么你觉得最舒服,并认识到为你获得的经验,你的preference可能会发生改变。有在能够同时读取和肯定会有的情况下,你必须使用这一点很有价值。其他情况下,将借给自己的人以一种风格比其他。最后,这一切都被转换成同一个可执行文件code。

So, use what you feel most comfortable with and realize that as you gain experience, your preference will likely change. There is great value in being able to read both and there will certainly be situations where you have to use a little bit of both. Other situations will lend themselve to one style over the other. In the end, it all gets translated into the same executable code.

这篇关于LINQ:点表示法VS查询防爆pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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