Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ中Intersect()的不同版本_C#_Linq - Fatal编程技术网

C# LINQ中Intersect()的不同版本

C# LINQ中Intersect()的不同版本,c#,linq,C#,Linq,有一个用户实体,我有一个用户对象列表。用户实体如下所示: public class User{ public int Id {get; set;} public List<Group> Groups {get; set;} } public class Group{ public int Id {get; set;} public string GroupName {get; set;} } List<string> GroupNames; 最后,我有一

有一个用户实体,我有一个用户对象列表。用户实体如下所示:

public class User{

public int Id {get; set;}

public List<Group> Groups {get; set;}

}
public class Group{

public int Id {get; set;}

public string GroupName {get; set;}

}
List<string> GroupNames;
最后,我有一个字符串列表,如下所示:

public class User{

public int Id {get; set;}

public List<Group> Groups {get; set;}

}
public class Group{

public int Id {get; set;}

public string GroupName {get; set;}

}
List<string> GroupNames;

你能帮我写这个查询吗?非常感谢您的帮助,谢谢。

Linq应该将any与Contains结合起来,轻松解决这个问题


Linq应该通过将Any与Contains相结合来轻松解决这个问题

您可以使用任意一个,并包含:

这与傅波的回答几乎相同

希望有帮助

您可以使用任意一个并包含:

这与傅波的回答几乎相同


希望有帮助

另一种可能是使用Intersect+Any,但请注意使用.Selectx=>x.GroupName仅选择GroupName


另一种可能是使用Intersect+Any,但请注意使用.Selectx=>x.GroupName仅选择GroupName


这个查询可以在异步方法中使用吗?@jason是的,确保它可以在我的代码中得到这个错误:Enumerable。不支持任何方法。我可以不用任何方法来做这件事吗?非常感谢。您是使用System.Linq添加的吗?我在AsyncRun方法中遇到此错误。这里面没有查询。这个查询可以在异步方法中使用吗?@jason是的,确保它可以在我的代码中得到这个错误:Enumerable。不支持任何方法。我可以不用任何方法来做这件事吗?非常感谢。您是使用System.Linq添加的吗?我在AsyncRun方法中遇到此错误。这是毫无疑问的。
var filteredUsers = Users.Where(x => x.Groups.Select(x => x.GroupName).Intersect(GroupNames).Any());