LINQ 子集合子选择

发布于 2024-09-15 17:03:43 字数 887 浏览 4 评论 0原文

您好 – 我正在尝试获取适用于子集合的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独行侠 2024-09-22 17:03:43

这样做:

var oResult = (from item in People
                where item.Skills.Count() == 1 &&
                item.Skills.Any(s => s.SkillName == "LINQ")
                select item
    ).ToList();

这个查询不会返回任何内容,因为 p2(您)除了 LINQ 之外还有其他技能。

您可以按照以下方式执行您想要的操作:

foreach (var person in oResult)
{
    person.Skills.RemoveAll(s => !s.SkillName.Equals("LINQ"));
}

注意:使用 LINQ 时,您只是过滤数据。要对其进行后处理,您可以使用类似我上面向您展示的 foreach 之类的东西。

Do this:

var oResult = (from item in People
                where item.Skills.Count() == 1 &&
                item.Skills.Any(s => s.SkillName == "LINQ")
                select item
    ).ToList();

This query will return nothing because p2 (You) has other skills in addition to LINQ.

You can do what you want this way:

foreach (var person in oResult)
{
    person.Skills.RemoveAll(s => !s.SkillName.Equals("LINQ"));
}

Note: while using LINQ you're only filtering your data. To post process it you use something like the foreach I show you above.

与之呼应 2024-09-22 17:03:43

试试这个:

var oResult = (from item in People
               where item.Skills != null
               where item.Skills.Count() > 0
               where item.Skills.All(s => s.SkillName == "LINQ")
               select item
              ).ToList();

即使您的示例显示 Skills 集合具有值,但您仍希望确保如果 Skills 属性为 时您的代码不会崩溃>空。

此外,如果列表为空,All 谓词将返回 true,因此您需要确保它不为空。上面的查询读起来更好,但根据调用 Count()Skills 属性的实现,可能会导致整个集合被迭代。您可以使用 Any() 来确保集合不为空。

var oResult = (from item in People
               where item.Skills != null
               where item.Skills.Any()
               where item.Skills.All(s => s.SkillName == "LINQ")
               select item
              ).ToList();

Try this:

var oResult = (from item in People
               where item.Skills != null
               where item.Skills.Count() > 0
               where item.Skills.All(s => s.SkillName == "LINQ")
               select item
              ).ToList();

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 the Skills property is null.

Also, the All predicate returns true 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 the Skills property calling Count() may cause the entire collection to be iterated. You could use Any() instead to ensure that the collection is not empty.

var oResult = (from item in People
               where item.Skills != null
               where item.Skills.Any()
               where item.Skills.All(s => s.SkillName == "LINQ")
               select item
              ).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文