用于过滤列表中的 id 的 Linq 查询 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18223213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linq query to filter id inside a list of list c#
提问by Sajeetharan
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.
我有一个结果列表列表,其中包含 List。我有另一个列表,其中仅包含 List。我想使用 linq 查询从数据中进行过滤,它应该返回包含第二个技能 ID 的所有数据列表。
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.
从第一个列表开始,它包含技能列表中的小数列表;item.Skills 包含字段为技能 ID 和代码的列表。item 是另一个包含技能列表的对象。
采纳答案by nerdybeardo
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.
如果 SkillId 是一个变量并假设 SkillsList 包含一个名为 Id 的属性。然后,以下内容可以帮助您获取具有指定技能 ID 的任何数据。
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.
如果 Skillslist 只是一个整数数组,那么以下方法将起作用。
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));