我有 2 张 table :
Table1: Id_tb1 (PK), Title1.
Table2: Id_tb2 (PK), Id_tb1 (FK),Title2.
所以table2的简单查询可以这样写:
from p in table2 select new { p.Id_tb2, Title1 = p.Table1.Title1 }
当使用 group by FK 列时,如何在我的 Select 中获取 Title1?像这样的东西:
from p in table2 group p by p.Id_tb1 into g select g.Table1.Title1
最佳答案
试试这个:
var result =
from p in table2
group p by p.Table1 into g
select g.Key.Title1;
这按 Table1
实体进行分组,而不仅仅是 ID。
g.Key
允许您访问组 key ,它是每个组的 Table1
实体。
关于c# - 按外键分组并选择 (LINQ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34813876/