0

I have this List of IDS

var profileID=(PMSdb.Tbl1.Where(x=>x.ID==_ID)).ToList();

and I want to Get from database based on this List so I used

   var Profiles = new List<Profile>(crmcontext.Profile.Where(x=>profileIDs.Contains(x.Profile_ID)));

it works well but it will get all Id's which contains this value not the exact value for example if my ProfileIDs list is as [1,2,5] and profile Table ID is [12,1,25,112]

it will get all of them not only ID=1

how can I get this?

Nesreen adly
  • 45
  • 1
  • 2
  • 10
  • This looks right on the face of it. Contains should check for equality between all elements of the list. Assuming nothing weird and based on the two example arrays this code should work – ste-fu Aug 24 '17 at 08:40
  • @ste-fu Sorry, I can't understand your point, did you mean that it will return only the exact value ? if yes how!! contains function works as `Like` function in SQL – Nesreen adly Aug 24 '17 at 08:47
  • To me @ste-fu is right, this should work. I thought "contains" would be converted to an "in" clause ... – Mathieu VIALES Aug 24 '17 at 08:50
  • According to [this answer by the mighty Jon Skeet](https://stackoverflow.com/a/1075564/6838730) your code should work ... – Mathieu VIALES Aug 24 '17 at 08:53

1 Answers1

0

Edit: Your code should work perfectly, ignore what is below...

I just tried this in a test project

var ids = new List<int>{1, 2, 5, 10, 25};
var targetIds = new List<int>{1, 1010, 5, 10, 255};
var Profiles = targetIds.Where(x=>ids.Contains(x));

foreach(var p in Profiles)
{                
    Console.WriteLine(p);
}

and it outputs

1
5
10

which means it did not take the 1010 value. Are you sure that in your example you get wrong rows ? If you do, it comes from the SQL, not LINQ.

It means that instead if you do the following it should work

var Profiles = new List<Profile>(crmcontext.Profile.ToList().Where(x=>profileIDs.Contains(x.Profile_ID)));

WARNING THIS WILL CONVERT YOUR WHOLE TABLE INTO A LIST - DO NOT USE IF THE TABLE CONTAINS LOTS OF DATA

Mathieu VIALES
  • 4,526
  • 3
  • 31
  • 48