gpt4 book ai didi

c# - 林克 : doing a Where() clause on a nested model

转载 作者:太空狗 更新时间:2023-10-30 00:28:06 25 4
gpt4 key购买 nike

这可能看起来很复杂,但我实际上只是想从 A 中选择所有记录及其子项,其中子项存在某些条件。寻找我可以添加到 A 变量的语法,它允许我过滤一堆条件(比如规范模式?)

如果你有这样的嵌套 View :

var a = from Arow in Atable
where ???
select new AViewModel { // (image Products)
id = Arow.id,
name = Arow.Name,
Brows = (from Brow in Arow.Brows
select new BViewModel { // (sold in different regions)
id = Brow.id,
name = Brow.Name,
Crows = (from Crow in Brow.Crows
select new CViewModel { // (and in many stores)
id = Crow.id,
name = Crow.Name
}
}
};

以及表示来自这样的网页的查询的文本(规范模式?)我们为运算符使用了 JQuery 选择器(比如 ^= 表示“开始于”)

filter[] = { "Brow.Name = Joe", "Crow.Name = Kim" }

您如何使用这些条件过滤表达式 A?换句话说,你能不能有一个像 a.Where() 这样可以过滤嵌套属性的 where 表达式?

我糟糕的尝试:

var b = from row in a
where a.Brow.Where(b => b.name == "Joe") &&
a.Brow.Crow.Where(c => c.name == "Kim")
select row;

我真正需要的是这样的:

Select * 
from A join B on A.key = B.key join C on B.key = C.key -- propagated keys
where exists (select null from B where A.key = B.key and B.Name = "Joe") and
exists (select null from C where A.key = C.key and C.Name = "Kim")

特别是如果我能做到这一点:

var result = a.Where(first).Where(second);

最佳答案

您的“糟糕尝试”离我们不远了。只需将 Any() 替换为 Where():

var b = from row in a
where a.Brow.Any(b => b.name == "Joe") &&
a.Brow.Crow.Any(c => c.name == "Kim")
select row;

编辑添加:

不过,您可能想要进行不区分大小写的比较,例如 .Any(b => b.name.Equals("Joe", StringComparison.CurrentCultureIgnoreCase))

关于c# - 林克 : doing a Where() clause on a nested model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4070981/

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