gpt4 book ai didi

entity-framework - Linq 选择子对象不包含值的父对象

转载 作者:行者123 更新时间:2023-12-02 17:22:15 26 4
gpt4 key购买 nike

如果我有这样的数据库结构

class Parent {
int ID { get; set; }
string Name { get; set; }
List<Child> Children { get; set; }
}

class Child {
int ID { get; set; }
string Name { get; set; }
List<Parent> Parents { get; set; }
}

我想得到所有 child 名字不是“Dave”的 parent 的名单

所以如果 parent 一有两个 child 叫 jack 和吉尔,我想要他们

如果 parent 二有两个 child 叫 Sam 和 Dave 我不想要他们

如果 parent 三没有 child 我想要他们

如果 parent 四有一个 child 叫史蒂夫,我想要他们

如果 parent 五有一个 child 叫戴夫,我不想要他们

最佳答案

I want to get a list of all the Parents who have a child whos name is NOT "Dave"

与规则 #3 冲突:如果 parent 三没有 child ,我想要他们

符合所有规则的要求可以描述为:

get a list of all the Parents who have NO child WITH name "Dave"

在 LINQ 中翻译为:

var query = db.Parent.Where(p => !p.Children.Any(c => c.Name == "Dave"));

关于entity-framework - Linq 选择子对象不包含值的父对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41769441/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com