- 我的表名为 CLASS ,其中包含以下记录
(PK)
ClassID | Name
----------|----------
1 | CSE
2 | ECE
我还有另一个表命名为 ALLOCATIONS ,其 CLASS 表格中包含ForeighnKey Class_ID
(PK) (FK)
ID | Class_ID | EffectiveFrom | Allocation
-----|--------------|------------------|---------------
1 | 1 | 10-May-2011 | A
2 | 1 | 10-May-2011 | B
3 | 1 | 14-June-2011 | A
---------------------------------------|---------------
4 | 2 | 14-June-2011 | C
5 | 2 | 14-June-2011 | D
6 | 2 | 17-June-2011 | C
我有 CSE & ECE IQueryable [CLASS] 变量中的类,现在我需要基于最高 EffectiveFrom 日期的以下分配表记录,如下所示:
(PK) (FK)
ID | Class_ID | EffectiveFrom | Allocation
-----|--------------|------------------|---------------
2 | 1 | 10-May-2011 | B
3 | 1 | 14-June-2011 | A
---------------------------------------|---------------
5 | 2 | 14-June-2011 | D
6 | 2 | 17-June-2011 | C
如何在LINQ中执行此操作(在LAMBDA表达式中优先),我需要 AllocationType 中的结果而不是 AnaymousType
谢谢!
此致 普拉迪普
答案 0 :(得分:1)
尝试:
from allocation in CurrentClasses.Allocations
group allocation by allocation.Class_ID
into allocationGroups
from allocationGroup in allocationGroups
from allocationRow in allocationGroup
where allocationRow.Date == allocationGroup.Max(x => x.Date)
select allocationRow;
希望有帮助,抱歉查询语法,但我对lambda中的连接没有太多经验。
答案 1 :(得分:0)