gpt4 book ai didi

c# - LINQ 查询中的多层外键

转载 作者:行者123 更新时间:2023-11-30 22:47:12 24 4
gpt4 key购买 nike

如果我的 Table3 有一个 FK 指向 Table2,而 FK 有一个 FK 指向 Table1 我通过智能感知看到的是我可以从 Table3 引用 Table2 但我不能去 Table3.Table2.Table1 它只会去一个层深。

from t3 in Table3
where t3.t2.property == "some value" && t3.t2.t1.property == "some other value"
select t3.t2.t1;

这基本上是我想做的,但我只能引用 t2,而不能引用 t2 链接到的 t1。

我应该这样做吗:

from t3 in Table3
from t1 in Table1
where t3.t2.property == "some value" && t1.property == "some other value"
select t1;

?

最佳答案

你可以连接所有的表:

from t3 in Table3
join t2 in Table2 on t3.Table2_FK equals t2.ID
join t1 in Table1 on T2.Table1_FK equals t1.ID
where t2.property == "some value" && t1.property == "some other value"
select t1;

(编辑)

我不会只深入一层。事实上,你的第一个例子应该有效。当然,你的关系必须是 N 对 1:

Table3 (n) --- (1) Table2 (n) --- (1) Table1

给定 Table3t3 你可以:

t3.Table2.Table1

.dbml 文件中的 Table2Table1 之间是否有正确的连接?

关于c# - LINQ 查询中的多层外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2318418/

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