【发布时间】:2011-10-18 20:50:59
【问题描述】:
我正在尝试重现一个非常基本的 SQL 查询:
SELECT t1.id, t1.name, count(*), min(t2.inserted) as inserted_first, max(t2.inserted) as inserted_last
FROM tbl1 t1
LEFT JOIN tbl2 t2 ON t1.id=t2.tbl1_id
WHERE t2.search=15
GROUP BY t1.id, t1.name
这对我来说很完美。它允许我按 t1.id 和 t1.name 的唯一项进行分组,还可以获取这对出现的次数,以及链接表的 t2.inserted 的最小值和最大值。问题是,现在当我把它变成 LINQ 时,我得到:
Dim query =
From t1 In ent.TBL1
Join t2 In ent.TBL2 On t1.id Equals t2.tbl1_id
Where (t2.search=15)
Group t1 By t1.id, t1.name Into Group
Select New With {
.id = id,
.name = name,
.count = Group.Count,
.min_value = ???,
.max_Value = ???
}
我不知道我可以做些什么来选择最小值和最大值。如果 Group.Min 与分组在同一个表中,它会起作用,但是由于它在 t2 中,我无法引用它。我也不能将它添加到我的组中,因为它不同。
请注意,tbl2 链接到 tbl2.tbl1_id -> tbl1.id 上的 tbl1。这也是我的问题的一个简化示例,而不是真正的实时模式。
感谢您对此事的任何帮助
【问题讨论】:
标签: linq linq-to-entities