C# 使用LINQtoSQL查找公共数据

C# 使用LINQtoSQL查找公共数据,c#,linq,C#,Linq,我正在使用LINQtoSQL从mvc项目中的IQueryable对象中提取数据。在本例中,我尝试查找用户有共同点的相册。谢谢你的帮助 Albums.Where(x=>x.userid == _userid && x.userid == _otheruserid); //This will pull all the respected albums from each user. //I just would like to pull the albums they hav

我正在使用LINQtoSQL从mvc项目中的IQueryable对象中提取数据。在本例中,我尝试查找用户有共同点的相册。谢谢你的帮助

Albums.Where(x=>x.userid == _userid && x.userid == _otheruserid);
//This will pull all the respected albums from each user. 
//I just would like to pull the albums they have in common
你可以用。很简单:

Albums.Where(x=>x.userid ==_userid) 
.Intersect(Albums.Where(x=>x.userid== _otheruserid));
编辑:
对于您提供的代码,您希望使用特定字段进行匹配,因为您不能在一个相册中分配两个不同的用户。如果要查找基于名称的常用相册,可以尝试使用以下方法:

 Albums.Where(x=>x.userid ==_userid).Select(x=>x.Name) 
    .Intersect(Albums.Where(x=>x.userid== _otheruserid).Select(x=>x.Name)); 
但是正如@EvilPenguin提到的,您最好为您的表创建一个更好的结构。

您可以使用。很简单:

Albums.Where(x=>x.userid ==_userid) 
.Intersect(Albums.Where(x=>x.userid== _otheruserid));
编辑:
对于您提供的代码,您希望使用特定字段进行匹配,因为您不能在一个相册中分配两个不同的用户。如果要查找基于名称的常用相册,可以尝试使用以下方法:

 Albums.Where(x=>x.userid ==_userid).Select(x=>x.Name) 
    .Intersect(Albums.Where(x=>x.userid== _otheruserid).Select(x=>x.Name)); 

但是正如@EvilPenguin所提到的,您最好为您的表创建一个更好的结构。

相册是否有唯一的标识符?通常情况下,您会使用三个表:用户表、相册表和链接其他两个的用户相册表。问题中的代码是否按预期运行<代码>相册。其中(x=>x.userid==\u userid&&x.userid==\u otheruserid)仅在
\u userid==\u otheruserid
时检索相册,这意味着相同的用户。我认为您提供的查询不正确,它应该是| |而不是&&相册是否具有唯一标识符?通常情况下,您会使用三个表:用户表、相册表和链接其他两个的用户相册表。问题中的代码是否按预期运行<代码>相册。其中(x=>x.userid===\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\当
\\\\\\\\\\\\\\\\\\\\\\\,这两个相册将是相册中的不同对象userid@TheEvilPenguin,是的,你可能是对的。OP可能希望根据特定属性查找它们。让我更新我的答案。给定代码,我假设在模型中,如果User1和User2都有相同的相册,那么这两个相册将是相册中不同的对象userid@TheEvilPenguin,是的,你可能是对的。OP可能希望根据特定属性查找它们。让我更新我的答案。