1

I have a list of ids:

var IdList = new int[]{1, 2};

I also have a list of users:

var userList = new List<User>()
        {
            new User() {Id = 1, Name = "User1" },
            new User() {Id = 1, Name = "User2" },
            new User() {Id = 2, Name = "User2" },
            new User() {Id = 1, Name = "User3" },
            new User() {Id = 1, Name = "User4" },
            new User() {Id = 2, Name = "User4" }
        };

I want to get a list of users that must contain all the Ids from the IdList. So in this example I want to return User2 and User4. I've seen other subset examples that are just using Except and return boolean and even when adapting to my needs do not produce the correct results. I also saw one marked as duplicate (Similar Question) which is trying to do exactly this but did not agree that it was a duplicate and was never truly answered. I have attempted:

userList.Where(u => IdList.All(i => i == u.Id)).ToList();

that will not return anything.

Community
  • 1
  • 1
user1015196
  • 617
  • 1
  • 7
  • 24
  • try using a `GroupBy`. – Daniel A. White Aug 04 '16 at 19:13
  • Why do you have 2 users with same Id ? User1,User2 and User3 all has Id 1, Your expected result in your question (So in this example I want to return User2 and User4.) does not makes sense! – Shyju Aug 04 '16 at 19:15
  • 1
    I agree with Shyju. Your data model seems very odd. If you have control over it, you should create user objects with unique IDs. Then change the Name property into a List Names (or an array if known, fixed length). – Christoph Aug 04 '16 at 19:17
  • Sorry for the confusion. It is just an example the Id could be a locationId or some other identifying Id property where user can have a 1 to many relationship – user1015196 Aug 04 '16 at 19:39

3 Answers3

1

Your question is a little confusing. You say you want to get a list of users that must contain all the Ids from the IdList and your expected output is User 2 and 4. That does not makes sense because your IdList has 1 and 2.

Also you have more than one record with Id 1. User1, User2 and User3 has same Id. What is the pattern to get one record from this ?

Assuming you do not have duplicate data and you want subset of items based on the items int eh idList, You can use LINQ Contains method.

var IdList = new int[]{1, 2};
var userList = new List<User>()
        {
            new User() {Id = 1, Name = "User1" },
            new User() {Id = 2, Name = "User2" },
            new User() {Id = 3, Name = "User3" },
            new User() {Id = 4, Name = "User4" }
        };
var subSet = userList.Where(d=>IdList.Contains(d.Id);

//subSet  will have records for  User1 and User2 
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • as I stated in another comment the model and Id was just an example. It could be a productId or locationId so I would want only the users that contain all the products or are associated to all the locations. Your code would bring anyone that contains any of the Ids passed in which is not what I am attempting to do. They must be associate with all the Ids in the list. Hope that clarified it for you. – user1015196 Aug 04 '16 at 23:06
0

Well, this is not elegant, but works:

List<string> result = new List<string>();
result = userList.Where(x => x.Id == IdList[0]).
          Select(x => x.Name).ToList();

for(int i =1; i<IdList.Count();i++)
{
    result = userList.Where(x => x.Id == IdList[i]).
          Select(x => x.Name).ToList().
          Intersect(result).ToList();
}
Magnetron
  • 7,495
  • 1
  • 25
  • 41
0

Use Bellow 1Line linq code.

var q = userList.GroupBy(c => c.Name).Where(c => IdList.All(ccc => userList.Where(cc => cc.Name == c.Key).Any(cc => cc.Id == ccc))).ToList();

and this code return User2 and User4

hamid_reza hobab
  • 925
  • 9
  • 21
  • that did it and was what I was looking for. I was almost there with the where statement but missed the groupby and including the key. My model is also more complex but this got me in the right direction. thank you! – user1015196 Aug 04 '16 at 23:09