Linq query to filter id inside a list of list c#

2019-02-16 10:33发布

问题:

I have a list of result List where it contains List inside of it.I have another list where it contains List alone.I want to filter using a linq query from the data where it should return all the data which contains skill id from the second list.

var list = this._viewModel.Data.Select(T => T.SkillsList);
var filtered = item.Skills.Contains(list.Where(t=>t.ToString()).ToList();

from the first list it contains List of decimals inside the skill list; item.Skills contains list where the fields are skillid and code. item is another object which contains the skillslist.

回答1:

if skillId is a variable and assuming that SkillsList contains a property called Id. Then the following would work in getting you any data that has the specified skillId.

var list = this._viewModel.Data.Where(t=>t.SkillsList.Any(s=>s.Id == skillId));

If Skillslist is just an array of integers then the following would work.

var list = this._viewModel.Data.Where(t=>t.SkillsList.Any(s=> s == skillId));

Now if you are checking against a list the following would work.

var list = this._viewModel.Data.Where(t=>t.SkillsList.Any(s=> skillsList.contains(s));