我有一个linq查询,我需要根据testTypeId,testTakerId,subAreaId,subjectName,testDate.Value.Month,testDate.Value.Year在一个月内选择最高测试日期进行分组。
我遇到的问题是嵌套列表的关注点。有没有办法将数据展平为testTakerId,testTypeId,subAreaId,subjectName,testDate.Value.Month,testDate.Value.Year。
非嵌套值(testTakerId,test.Value.Month等)工作正常,但是嵌套(subAreaId)值我遇到了麻烦。
var q1 = from entry in result
let testDate = entry.result.TestDate
where testDate != null
group entry by new { entry.testTakerId, entry.testInstance.Select(
sr => sr.Subject.Select(c => c.subArea.Id)), entry.testInstance.Select(
sr => sr.Subject.Select(c => c.subArea.Name)),entry.testInstance.Select(
sr => sr.testInstance.Test.TestType.Id), testDate.Value.Month,
testDate.Value.Year } into g
select g.Where(entry => entry.result.TestDate == g.Max(e => e.result.TestDate));
答案 0 :(得分:1)
您必须首先通过加入包含的集合(testInstance
和Subject
)来展平结果集,然后进行分组:
var q1 = from entry in result
from ti in entry.testInstance // translates to a SQL join
from su in ti.Subject // translates to a SQL join
let testDate = entry.result.TestDate
where testDate != null
group entry by new
{
entry.testTakerId,
su.subArea.Id,
su.subArea.Name,
ti.Test.TestType.Id,
testDate.Value.Month,
testDate.Value.Year
}
into g
select g.Where(entry => entry.result.TestDate ==
g.Max(e => e.result.TestDate));
在流利(或方法)语法中,from ti in entry.testInstance
之类的语句等同于SelectMany
。