gpt4 book ai didi

c# - 我如何在 Entity Framework 中执行此操作(多个 where's 或 Join)?

转载 作者:太空狗 更新时间:2023-10-29 22:35:19 25 4
gpt4 key购买 nike

我有 2 个相互关联的表

表 A 与表 B 具有一对多的关系,因此这将为每个表创建一个导航属性。

现在我需要检查表 A 中的值 (userName) 和表 B 中的值 (ClubId)。

所以在我看来应该是这样的

Join the tables together
Where A.userName == "bob" &&
where B.clubId == "Car"

// return the count.

但现在我知道对于 Entity 的东西它应该使连接不那么常见所以我想知道我是否可以通过连接来做到这一点。

我试过了

int count = Entity.TableA.where(a => a.userName == "bob" && a.TableB.where(i => i.ClubId == "Car")).Count();

所以这不起作用,因为它不会返回正确的类型(第二个位置)。这就是我的想法,我希望它能完成。

那么它应该是什么样子呢?

附言

我宁愿像上面那样在 Linq 方法查询中完成一个示例。

最佳答案

假设您的 EF 模型具有 Users 和 Clubs 之间的关系,可以执行如下操作:

var usersNamedBobInCarClub = 
from A in User
from B in A.Clubs
where A.userName == "bob" &&
B.clubId == "Car"
select A;

如果您想要 Users 和 Clubs 的返回元素,请查看查询中的连接。

关于c# - 我如何在 Entity Framework 中执行此操作(多个 where's 或 Join)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1269696/

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