lambda 表达式或

发布于 2024-08-02 05:21:28 字数 887 浏览 1 评论 0原文

我有以下 LINQ to SQL 查询表达式

from msg in TblUserMessages 
join user in Aspnet_Users on msg.FromUserID equals user.UserId
select new {
       msg.FromUserID, 
       msg.ToUserID, 
       msg.MessageLocationID, 
       msg.MessageID, 
       user.UserName
       }

和以下 LINQ 方法表达式:

TblUserMessages
.Join (
  Aspnet_Users, 
  msg => msg.FromUserID, 
  user => user.UserId, 
  (msg, user) => 
     new  
     {
        FromUserID = msg.FromUserID, 
        ToUserID = msg.ToUserID, 
        MessageLocationID = msg.MessageLocationID, 
        MessageID = msg.MessageID, 
        UserName = user.UserName
     }
)

这两个都返回相同的结果集。例如:

82522f05-2650-466a-a430-72e6c9fb68b7
6b2a174a-8141-43d2-b3ad-5b199bcbfcae
1
1
waheed

哪个更好用。 FIRST 一个或 SECOND 一个。

谢谢

I have following LINQ to SQL query expression

from msg in TblUserMessages 
join user in Aspnet_Users on msg.FromUserID equals user.UserId
select new {
       msg.FromUserID, 
       msg.ToUserID, 
       msg.MessageLocationID, 
       msg.MessageID, 
       user.UserName
       }

And following LINQ method expression:

TblUserMessages
.Join (
  Aspnet_Users, 
  msg => msg.FromUserID, 
  user => user.UserId, 
  (msg, user) => 
     new  
     {
        FromUserID = msg.FromUserID, 
        ToUserID = msg.ToUserID, 
        MessageLocationID = msg.MessageLocationID, 
        MessageID = msg.MessageID, 
        UserName = user.UserName
     }
)

Both of these return same result set. Which is e.g. :

82522f05-2650-466a-a430-72e6c9fb68b7
6b2a174a-8141-43d2-b3ad-5b199bcbfcae
1
1
waheed

Which one is better to use. The FIRST one or the SECOND one.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

雨巷深深 2024-08-09 05:21:28

它们是等价的。它们不只是返回相同的结果集 - 它们编译为相同的代码。

根据可读性,对个别情况使用查询表达式或点表示法。对于连接,我发现点表示法相当麻烦 - 但我将它用于只有一两个子句(通常是 where/select)的情况。即使有两个子句(通常是 选择),如果您无论如何都需要使用它,我发现点表示法很好。例如,我喜欢:

var query = people.Where(person => person.Age > 18)
                  .Select(person => person.Name)
                  .Skip(100)
                  .Take(10);

over

var query = (from person in people
             where person.Age > 18
             select person.Name)
            .Skip(100)
            .Take(10);

对于更复杂的查询(例如联接),我可能只是将两者分开:

var baseQuery = from person in people
                where person.Age > 18
                join company on person.CompanyId equals company.CompanyId
                select new { person.Name, company.Name };

var fullQuery = baseQuery.Skip(100)
                         .Take(10);

我只是发现这种分离使其更易于阅读。

我相信,对于开发人员来说,至少了解查询表达式的基本功能非常有用 - 事实上,它们基本上是点表示法的翻译,而且语言本身对 LINQ to Objects、LINQ to SQL 等一无所知;这只是遵循适当模式的情况。这是一个很棒的设计,这意味着查询表达式只影响语言规范的一小部分。

They are equivalent. They don't just return the same result set - they compile to the same code.

Use query expressions or dot notation for individual cases depending on readability. For joins, I find the dot notation quite cumbersome - but I use it for situations where I only have one or two clauses (usually where/select). Even with two clauses (usually where and select) I find dot notation nice if you then need to use it anyway. For example, I like:

var query = people.Where(person => person.Age > 18)
                  .Select(person => person.Name)
                  .Skip(100)
                  .Take(10);

over

var query = (from person in people
             where person.Age > 18
             select person.Name)
            .Skip(100)
            .Take(10);

For more complicated queries (e.g. joins) I'd probably just separate the two:

var baseQuery = from person in people
                where person.Age > 18
                join company on person.CompanyId equals company.CompanyId
                select new { person.Name, company.Name };

var fullQuery = baseQuery.Skip(100)
                         .Take(10);

I just find this separation makes it easier to read.

I believe it's really useful for developers to understand at least the basics of what query expressions do - the fact that they're basically translations into dot notation, and that the language itself doesn't know anything about LINQ to Objects, LINQ to SQL etc; it's just a case of following an appropriate pattern. It's a great bit of design, which means query expressions only affect one little bit of the language specification.

日暮斜阳 2024-08-09 05:21:28

没有更好了。使用你喜欢的。在这种情况下,我会使用“查询”语法,因为我认为它比第二个语法更具可读性。此外,由于我们无论如何都在编写 SQL,所以我认为这种语法更类似于它。但我确信其他人会不同意并倾向于选择 lamda 版本。

顺便说一句,我通常使用后一种语法,因为我更喜欢 lambda 语法,因为它通常更易读且更短。

另请参阅这个问题

There's no better one. Use what you like. I would, in this case, go with the 'query' syntax, since I think it's more readable than the second one. Moreover, since we're writing SQL anyway, I think this syntax resembles it better. But I'm sure others will disagree and tend to choose the lamda version.

I usually go with the latter syntax btw, since I prefer the lambda syntax cause it is often more readable and shorter.

See also this SO question.

笑饮青盏花 2024-08-09 05:21:28

我总是选择更具可读性的选项,在本例中我认为它是 LINQ 片段。

I'd always go for the more readable option and in this case I think its the LINQ snippet.

清泪尽 2024-08-09 05:21:28

两者的性能都一样好,但我更喜欢第一个,因为它更容易阅读和理解。

Both will perform equally well, but I will prefer the first since it's easier to read and understand.

山色无中 2024-08-09 05:21:28

无论您觉得哪个更容易阅读。

Whichever you find easier to read.

悲喜皆因你 2024-08-09 05:21:28

“编写代码,然后对其进行测量以找出需要重构的内容。”两者你都测量过吗?
更好是主观的。

您是否发现其中任何一个存在问题,导致您想要选择其中一个而不是另一个?

"write your code, then measure it to find out what to refactor." Have you measured both?
Better is subjective.

Are you finding a problem with either one that is making you want to choose one over the other?

烧了回忆取暖 2024-08-09 05:21:28

这两种语法之间有一点区别。查询表达式的编译时间将更长,因为编译器必须将其转换为点语法。当然,这几乎完全无关紧要,因为:

  1. 编译过度是最小的,
  2. 运行时需要处理的发出的 IL 在两种情况下都是完全相同的。

There IS one little difference between the two types of syntax. The query expression one will take longer to compile because the compiler has to transform it into the dot syntax. Of course, it is almost completely immaterial because:

  1. the compilation overhed is minimal,
  2. the emitted IL that runtime needs to deal with is exactly the same for both cases.
坏尐絯 2024-08-09 05:21:28

当我必须动态构建查询时,我使用 Lambda 表达式;当查询完全已知时,我使用 LINQ to SQL。

IQueryable<IncomeDetailsEntity> query;
if (!string.IsNullOrEmpty(regioncode))
{
    if (!string.IsNullOrEmpty(compcode))
    {
        query = db.IncomeDetailsEntities.Where(i => i.RegionCode == regioncode && i.CompanyCode == compcode);
    }
    else
    {
        query = db.IncomeDetailsEntities.Where(i => i.RegionCode == regioncode);
    }
}
else
{
    query = db.IncomeDetailsEntities;
}
return query.Select(i => new { i.RegionCode, i.Budget });

I use Lambda Expressions when I have to build query dynamically, and LINQ to SQL when query is fully known.

IQueryable<IncomeDetailsEntity> query;
if (!string.IsNullOrEmpty(regioncode))
{
    if (!string.IsNullOrEmpty(compcode))
    {
        query = db.IncomeDetailsEntities.Where(i => i.RegionCode == regioncode && i.CompanyCode == compcode);
    }
    else
    {
        query = db.IncomeDetailsEntities.Where(i => i.RegionCode == regioncode);
    }
}
else
{
    query = db.IncomeDetailsEntities;
}
return query.Select(i => new { i.RegionCode, i.Budget });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文