简体   繁体   English

如何查询列表并返回值,其中ID =数组中的任何值

[英]How to query a list and return values where ID = any value in an array

I have 2 collections. 我有2个收藏。 1 is an array of groupIds, and the other is an IEnumerable. 1是groupIds的数组,另一个是IEnumerable。

My user class contains another list of type IList, which denotes the groups the user belongs to: 我的用户类包含另一个类型为IList的列表,该列表表示用户所属的组:

 public class User
    {

        public  IList<Group> Groups { get; protected set; }
    }

And the Group class looks something like this: Group类看起来像这样:

public class Group{
      public long Group_Id { get; protected set; }
}

What I need to do is return a collection of all users who are in the groupIds array. 我需要做的是返回groupIds数组中所有用户的集合。 Ie, I would want something like: 即,我想要类似的东西:

usersRepository.Where(user => user.Groups.Where(group => groupIds.Contains(group.id))

Such that if my groupIds array has group id of 5 and 6, all users in the useresRepository which belong to group 5 and 6 are returned. 这样,如果我的groupIds数组的组ID为5和6,则返回useresRepository中属于组5和6的所有用户。

So far, the best I've been able to come up with is the following: 到目前为止,我能提出的最好的建议如下:

UserRepo.Users.AsEnumerable().Where(user =>
            user.GroupUsers.Where(wg => GroupIds.Contains(wg.Group_Id)));

But this is complaining since .Contains returns a boolean instead of my list, but I'm not sure how to fix that. 但这是令人抱怨的,因为.Contains返回一个布尔值而不是我的列表,但是我不确定如何解决这个问题。

My code doesn't work due to .Contains() causing the expression to return a boolean rather than a collection. 由于.Contains()导致表达式返回布尔值而不是集合,因此我的代码无法正常工作。 However, I'm not sure how else to check of the list of GroupIds nested within my User object contains the specified group ids. 但是,我不确定如何另外检查嵌套在User对象中的GroupId列表是否包含指定的组ID。

Try Any .This will give you all the users that have any of the group mentioned in the groupIds array 试试Any 。这将为您提供所有具有groupIds数组中提到的任何组的用户

UserRepo.Users.AsEnumerable().Where(user =>
            user.Groups.Any(wg => GroupIds.Contains(wg.Group_Id)));

So you have a sequence of Users , where every User has a sequence of Groups , where every Group has a GroupId . 因此,您具有一系列的Users ,其中每个User都有一个Groups序列,其中每个Group都有一个GroupId

You also have a sequence of GroupIds . 您还具有一个GroupIds序列。

IEnumerable <int> groupIds = 
IQueryable<User> users = ...

You want all Users , that have at least one Group with a GroupId that is in you sequence of GroupIds . 你希望所有的Users ,即至少有一个GroupGroupId是在你的序列GroupIds

Whenever you see "I want all items that have at least one ...", consider using Any : 每当您看到“我希望所有具有至少一个...的物品”时,请考虑使用Any

var result = users
    .Where(user => user.groups
                       .Select(group => group.groupId)
                       .Any(groupId => groupIds.Contains(groupId));

In words: from every user, take its collection of Groups. 换句话说:从每个用户那里获取其组的集合。 From every Group take the groupIds, and check if any of these groupIds is in collection groupIds. 从每个组中获取groupId,并检查其中的任何groupId是否在集合groupId中。 If so, keep this user in your result. 如果是这样,请将该用户保留在您的结果中。

This can be a little bit shorter: 这可以短一点:

var result = users.Where(user => user.groups.Any(group => groupIds.Contains(group.groupId));

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

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