Search code examples
c#linqasp.net-core

Asp.Net Core : Get the list values ​of a table by string Id with Linq?


I have a color table that contains values, and I have a string of IDs that are the IDs of this table.I want to return only values ​​whose ID is in the string of IDs

Table Color: enter image description here

string Ids :

1,3,5

I want to receive the values ​​whose IDs are 1, 3, and 5 as a list ?


Solution

  • With EF Core you have to use Contains:

    var ids = stringIds.Split(',')
       .Select(s => int.Parse(s))
       .ToArray();
    
    var colors = db.Colors
        .Where(x => ids.Contains(x.Id))
        .ToList();