简体   繁体   English

如何将lambda表达式转换为LINQ?

[英]How to convert a lambda expression to LINQ?

How can I convert this expression to LINQ? 如何将此表达式转换为LINQ?

var result = users.FirstOrDefault(x => x.Name == userName)?
    .Groups.FirstOrDefault(x => x.Group == userGroup);

I've started with: 我开始时:

var result = (from u in users
              where u.Name == userName
              select u).FirstOrDefault()?

My class is: 我的班级是:

public class User
{
    public string Name { get; set; }
    public IEnumerable<Group> Groups { get; set; }
}

When creating this query, I don't have a separate groups list with which I can make a join on 2 tables. 创建此查询时,我没有单独的groups列表,我可以使用该列表在2个表上进行连接。

But that's how far I managed to go. 但那是我走了多远。 Is it possible to do a join within the same query? 是否可以在同一个查询中进行连接?

I guess you want this: 我想你想要这个:

var result = (from g in ((from u in users
                          where u.Name == userName
                          select u).FirstOrDefault().Groups)
              where g == userGroup
              select g).FirstOrDefault();

What you mean is how to convert method (or fluent) syntax to query (or comprehension) syntax. 你的意思是如何将方法(或流利的)语法转换为查询(或理解)语法。 The first thing to note though is there's not one LINQ expression. 首先要注意的是没有一个LINQ表达式。 The statement consists of two LINQ statements... 该声明包含两个LINQ语句......

var user = users.FirstOrDefault(x => x.Name == userName);
var result = user?.Groups.FirstOrDefault(x => x.Group == userGroup);

...both of which can be written in query syntax, of which your starting point would be the first one. ...这两个都可以用查询语法编写,其中你的起点是第一个。

However, the statement can be rewritten as one LINQ statement using SelectMany : 但是,可以使用SelectMany将语句重写为一个LINQ语句:

var result = users.Where(x => x.Name == userName)
    .SelectMany(u => u.Groups.Where(g => g.Group == userGroup))
    .FirstOrDefault();

This statement can be rewritten in one query-syntax statement: 可以在一个查询语法语句中重写此语句:

var result = (from u in users
    where u.Name == userName
    from g in u.Groups
    where g.Group == userGroup
    select g).FirstOrDefault();

The advantage is that you don't need the null-propagation operator, which, by the way, you didn't apply sufficiently in your own statement. 优点是你不需要null-propagation操作符,顺便说一句,你没有在你自己的语句中充分应用。

One possible issue is that the results aren't necessarily identical. 一个可能的问题是结果不一定相同。 Originally you query a first user meeting a condition and of its groups a first group meeting another condition. 最初,您查询满足条件第一个用户及其组第一个组满足另一个条件。 The alternative query queries all users meeting a condition and from their groups the first one that meets another condition. 备用查询查询满足条件的所有用户,并从其组中查询满足另一个条件的第一个用户。 So the first query may not return a result where the second does (if the matching group is not from the first user). 因此第一个查询可能不返回第二个查询的结果(如果匹配组不是来自第一个用户)。

This may be an improvement or a flaw, I don't know. 这可能是一种改进或缺陷,我不知道。 If the first condition uniquely identifies users it doesn't matter; 如果第一个条件唯一地标识用户则无关紧要; the results will be the same. 结果将是一样的。 If it doesn't you may have to question its value because in a way it will return you a "random" user. 如果不是,你可能不得不质疑它的价值,因为在某种程度上它会给你一个“随机”的用户。 You may want to use a lambda expression that narrows down the search to one specific user. 您可能希望使用lambda表达式,将搜索范围缩小到一个特定用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM