LINQ 子集合子选择
您好 – 我正在尝试获取适用于子集合的 where 条件。我可以获得返回合适父母的标准。但是,我希望子集合也仅限于标准。
在我的示例代码中,我只想要具有“LINK”技能的人;另外,我只希望每个人的技能都等于“LINK”。也就是说,每个人的技能应该只有“LINK”。
提前致谢。
List<Skill> skills = new List<Skill>();
skills.Add(new Skill(){SkillName="ASP.NET"});
skills.Add(new Skill(){SkillName="C#"});
Person p1 = new Person(){ Name="Me", Skills=skills} ;
List<Skill> skills2 = new List<Skill>();
skills2.Add(new Skill(){SkillName="ASP.NET"});
skills2.Add(new Skill(){SkillName="C#"});
skills2.Add(new Skill(){SkillName="LINQ"});
Person p2 = new Person(){ Name="You", Skills=skills2} ;
List<Person> People = new List<Person>();
People.Add(p1);
People.Add(p2);
var oResult = (from item in People
from sk in item.Skills
where sk.SkillName == "LINQ"
select item
).ToList();
当我运行这个时。我得到 p2(正确),但我希望 P2 的技能仅等于 LINQ
Hello – I’m trying to get a where condition to apply to a sub collection. I can get the criteria to return the proper parents. However, I want the sub collection to be limited to the criteria as well.
In my example code, I only want people with “LINK” skills; also, I only want the skills for each person to equal “LINK.” That is, each person should only have “LINK” for their skills.
Thanks in advance.
List<Skill> skills = new List<Skill>();
skills.Add(new Skill(){SkillName="ASP.NET"});
skills.Add(new Skill(){SkillName="C#"});
Person p1 = new Person(){ Name="Me", Skills=skills} ;
List<Skill> skills2 = new List<Skill>();
skills2.Add(new Skill(){SkillName="ASP.NET"});
skills2.Add(new Skill(){SkillName="C#"});
skills2.Add(new Skill(){SkillName="LINQ"});
Person p2 = new Person(){ Name="You", Skills=skills2} ;
List<Person> People = new List<Person>();
People.Add(p1);
People.Add(p2);
var oResult = (from item in People
from sk in item.Skills
where sk.SkillName == "LINQ"
select item
).ToList();
When I run this. I get p2 (correct), but I want the skills of P2 to only equal LINQ
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样做:
这个查询不会返回任何内容,因为 p2(您)除了 LINQ 之外还有其他技能。
您可以按照以下方式执行您想要的操作:
注意:使用 LINQ 时,您只是过滤数据。要对其进行后处理,您可以使用类似我上面向您展示的
foreach
之类的东西。Do this:
This query will return nothing because p2 (You) has other skills in addition to LINQ.
You can do what you want this way:
Note: while using LINQ you're only filtering your data. To post process it you use something like the
foreach
I show you above.试试这个:
即使您的示例显示
Skills
集合具有值,但您仍希望确保如果Skills
属性为时您的代码不会崩溃>空。
此外,如果列表为空,
All
谓词将返回true
,因此您需要确保它不为空。上面的查询读起来更好,但根据调用Count()
的Skills
属性的实现,可能会导致整个集合被迭代。您可以使用Any()
来确保集合不为空。Try this:
Even though your example shows that the
Skills
collection has a value, you want to make sure that your code doesn't blow up if theSkills
property isnull
.Also, the
All
predicate returnstrue
if your list is empty so you need to ensure that it is not empty. The above query reads better, but depending on the implementation of theSkills
property callingCount()
may cause the entire collection to be iterated. You could useAny()
instead to ensure that the collection is not empty.