简体   繁体   English

如何将这些linq查询合并为一个?

[英]How can I combine these linq queries into one?

Being new to LINQ, I created a couple queries and would like to combine them into one, but I am not sure how to do it. 作为LINQ的新手,我创建了几个查询,并希望将它们组合成一个查询,但是我不确定该怎么做。 Here they are: 他们来了:

var u = dc.Users.Where(w => w.UserName == userName).SingleOrDefault();

var m = dc.Memberships.Where(w => w.UserId == u.UserId).SingleOrDefault();

m.PasswordQuestion = securityQuestion;
m.PasswordAnswer = securityAnswer;

dc.SubmitChanges();

dc.Users is the aspnet_Users table dc.Usersaspnet_Users
dc.Membership is the aspnet_Membership table dc.Membershipaspnet_Membership

Also, What is the difference between SingleOrDefault and FirstOrDefault ? 另外, SingleOrDefaultFirstOrDefault什么FirstOrDefault

Not sure if they have a relationship (they should). 不知道他们是否有关系(他们应该)。 If they do, then the linq-to-sql designer will give you a User property on the membership object (or the other way around). 如果他们这样做了,那么linq-to-sql设计器将为您提供成员资格对象上的User属性(或者相反)。 Then you can write something like this: 然后,您可以编写如下内容:

var membership = dc.Memberships.Where(x => x.User.UserName == userName).SingleOrDefault();

If they don't have a relationship you can write something like this: 如果他们没有关系,则可以这样写:

var membership = (from m in dc.Membership join u in dc.Users on u.UserId equals m.UserId
                  where u.UserName == userName
                  select u).SingleOrDefault();

The difference between SingleOrDefault() and FirstOrDefault() is that SingleOrDefault() assumes that there is no more then one item that matches the query. SingleOrDefault()和FirstOrDefault()之间的区别在于,SingleOrDefault()假定不存在与查询匹配的项。 If two items match the query then a exception will be thrown. 如果两项匹配查询,则将引发异常。 While if you use FirstOrDefault() and there is two items that match the query then the first one will be selected. 如果使用FirstOrDefault()并且有两个与查询匹配的项目,则将选择第一个。

SingleOrDefault means "if there are no elements, give me the default, if there is one, give me it, otherwise, throw an exception". SingleOrDefault的意思是“如果没有元素,请给我默认值;如果有元素,请给我默认值,否则,抛出异常”。 FirstOrDefault means "if there are no elements then give me the default, otherwise give me the first one". FirstOrDefault的意思是“如果没有元素,则给我默认值,否则给我第一个”。

As for your question about combining the queries -- why would you? 至于您关于合并查询的问题-为什么呢? If the code is working well as written, why change it? 如果代码按编写的方式工作良好,为什么要对其进行更改?

To answer a question you didn't ask: the usual way of combining queries is to use a query continuation . 要回答您没有提出的问题:组合查询的通常方法是使用查询延续 A common pattern is: 常见的模式是:

var q1 = from y in something 
         somequeryclauses;
var q2 = from x in q1 
         someotherquerqyclauses;

You could write this as one big query like this: 您可以将其编写为一个大查询,如下所示:

var q2 = from x in (from y in something 
                    somequeryclauses) 
         someotherqueryclauses;

which gets hard to read. 这很难读。 You can combine the two queries with a query continuation: 您可以将两个查询与查询延续结合在一起:

var q2 = from y in something 
         somequeryclauses 
         into x
         someotherqueryclauses;

Make sense? 说得通?

SingleOrDefault will throw an error if your sequence contains more than one element, FirstOrDefault does not. 如果您的序列包含多个元素,则FirstOrDefault将不引发SingleOrDefault错误。 In this case, you'd probably want FirstOrDefault, because users can have more than one membership. 在这种情况下,您可能需要FirstOrDefault,因为用户可以拥有多个成员身份。

var m = dc.MemberShips where(w => w.UserId.UserName == userName).FirstOrDefault();

I don't have Visual Studio in front of me, but it would be something like this: 我前面没有Visual Studio,但可能是这样的:

var m = dc.Memberships.Where(w => w.Users.UserName == userName).SingleOrDefault(); 

m.PasswordQuestion = securityQuestion; 
m.PasswordAnswer = securityAnswer; 

dc.SubmitChanges(); 

w.Users allows you to follow the foreign key link between the membership and users tables. w.Users允许您跟踪Membership和Users表之间的外键链接。

SingleOrDefault will throw an exception if more than one result is returned. 如果返回多个结果,SingleOrDefault将引发异常。 FirstOrDefault returns the first record only. FirstOrDefault仅返回第一条记录。

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

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