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
string Ids :
1,3,5
I want to receive the values whose IDs are 1, 3, and 5 as a list ?
CodePudding user response:
List<string> result = Datatable.Rows
.Cast<DataRow>()
.Where(x => listOfId.Contains((int)x["Id"])) //This part does the filtering of id
.Select(x => x["Name"])
.ToList()
CodePudding user response:
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();
CodePudding user response:
int[] ids = { 1, 3, 5 };
var colors = (from d in ids from c in Colors where d == c.Id select c).ToList();
CodePudding user response:
Since you wrote Linq
in the TAGs, I assume that you are using EntityFrameworkCore
or LINQ to SQL
or something similar.
In this case, this example will help you:
int[] ids = { 1, 2, 3 };
var nameList = db.Colors
.Where(x => ids.Contains(x.Id))
.Select(x => x.Name)
.ToList();
where db
is DbContext