我已经通过基于此nested group example的查询得到了一个嵌套的LINQ组,它为同一个DateTime创建了多个键。
var queryNestedGroups =
from lockAssignment in db.LockAssignment
where lockAssignment.UserId == userIdForUsername
group lockAssignment by lockAssignment.DateStart into lockAssignmentsByDateGroup
from lockAssignmentsByZoneGroup in
(from lockAssignment in lockAssignmentsByDateGroup
group lockAssignment by lockAssignment.Lock.Zone.ZoneName)
group lockAssignmentsByZoneGroup by lockAssignmentsByDateGroup.Key;
// Three nested foreach loops are required to iterate
// over all elements of a grouped group. Hover the mouse
// cursor over the iteration variables to see their actual type.
foreach (var outerGroup in queryNestedGroups)
{
Debug.WriteLine("outerGroup.Key = {0}", outerGroup.Key);
foreach (var innerGroup in outerGroup)
{
Debug.WriteLine("\tinnerGroup.Key: {0}", innerGroup.Key);
foreach (var innerGroupElement in innerGroup)
{
Debug.WriteLine("\t\tinnerGroupElement: {0}, innerGroupElement.DateStart: {1}", innerGroupElement, innerGroupElement.DateStart);
}
}
}
这是控制台输出:
outerGroup.Key = 03/11/2015 10:45:55
Jewelry: innerGroup.Key: {0}
innerGroupElement: System.Data.Entity.DynamicProxies.LockAssignment_F0A35C00917489FA2C4D63DCAECAAD7A1EC03543B62C35206BF0542C5EAF1D8A, innerGroupElement.DateStart: 03/11/2015 10:45:55
innerGroupElement: System.Data.Entity.DynamicProxies.LockAssignment_F0A35C00917489FA2C4D63DCAECAAD7A1EC03543B62C35206BF0542C5EAF1D8A, innerGroupElement.DateStart: 03/11/2015 10:45:55
Watches: innerGroup.Key: {0}
innerGroupElement: System.Data.Entity.DynamicProxies.LockAssignment_F0A35C00917489FA2C4D63DCAECAAD7A1EC03543B62C35206BF0542C5EAF1D8A, innerGroupElement.DateStart: 03/11/2015 10:45:55
outerGroup.Key = 03/11/2015 10:45:55
Watches: innerGroup.Key: {0}
innerGroupElement: System.Data.Entity.DynamicProxies.LockAssignment_F0A35C00917489FA2C4D63DCAECAAD7A1EC03543B62C35206BF0542C5EAF1D8A, innerGroupElement.DateStart: 03/11/2015 10:45:55
innerGroupElement: System.Data.Entity.DynamicProxies.LockAssignment_F0A35C00917489FA2C4D63DCAECAAD7A1EC03543B62C35206BF0542C5EAF1D8A, innerGroupElement.DateStart: 03/11/2015 10:45:55
我希望03/11/2015
只能看到一个outerGroup.Key,如下所示:
outerGroup.Key = 03/11/2015 10:45:55
Jewelry: innerGroup.Key: {0}
innerGroupElement: System.Data.Entity.DynamicProxies.LockAssignment_F0A35C00917489FA2C4D63DCAECAAD7A1EC03543B62C35206BF0542C5EAF1D8A, innerGroupElement.DateStart: 03/11/2015 10:45:55
innerGroupElement: System.Data.Entity.DynamicProxies.LockAssignment_F0A35C00917489FA2C4D63DCAECAAD7A1EC03543B62C35206BF0542C5EAF1D8A, innerGroupElement.DateStart: 03/11/2015 10:45:55
Watches: innerGroup.Key: {0}
innerGroupElement: System.Data.Entity.DynamicProxies.LockAssignment_F0A35C00917489FA2C4D63DCAECAAD7A1EC03543B62C35206BF0542C5EAF1D8A, innerGroupElement.DateStart: 03/11/2015 10:45:55
innerGroupElement: System.Data.Entity.DynamicProxies.LockAssignment_F0A35C00917489FA2C4D63DCAECAAD7A1EC03543B62C35206BF0542C5EAF1D8A, innerGroupElement.DateStart: 03/11/2015 10:45:55
innerGroupElement: System.Data.Entity.DynamicProxies.LockAssignment_F0A35C00917489FA2C4D63DCAECAAD7A1EC03543B62C35206BF0542C5EAF1D8A, innerGroupElement.DateStart: 03/11/2015 10:45:55
我有没有办法让outerGroup.Key更多,包括类似的日期?
提前致谢!
答案 0 :(得分:1)
你的代码似乎工作正常。我的猜测是日期不是很明显。
将顶级调试行更改为以下内容以打印毫秒(这是一个开始)。否则可能会有一些不同的东西让.NET相信日期不一样。
Console.WriteLine("outerGroup.Key = {0} {0:fff}", outerGroup.Key);
您可以使用以下代码
将代码更改为使用DateTime对象的更多特定属性进行分组group lockAssignment by new {
lockAssignment.Group.Year,
lockAssignment.Group.Month
} into lockAssignmentsByDateGroup
答案 1 :(得分:1)
通过使用Kati的建议here,我能够得到我想要的分组,如下所示:
outerGroup.Key = { year = 2015, month = 3, day = 11 }
Jewelry: innerGroup.Key: {0}
innerGroupElement: System.Data.Entity.DynamicProxies.LockAssignment_F0A35C00917489FA2C4D63DCAECAAD7A1EC03543B62C35206BF0542C5EAF1D8A, innerGroupElement.DateStart: 03/11/2015 10:45:55
innerGroupElement: System.Data.Entity.DynamicProxies.LockAssignment_F0A35C00917489FA2C4D63DCAECAAD7A1EC03543B62C35206BF0542C5EAF1D8A, innerGroupElement.DateStart: 03/11/2015 10:45:55
Watches: innerGroup.Key: {0}
innerGroupElement: System.Data.Entity.DynamicProxies.LockAssignment_F0A35C00917489FA2C4D63DCAECAAD7A1EC03543B62C35206BF0542C5EAF1D8A, innerGroupElement.DateStart: 03/11/2015 10:45:55
innerGroupElement: System.Data.Entity.DynamicProxies.LockAssignment_F0A35C00917489FA2C4D63DCAECAAD7A1EC03543B62C35206BF0542C5EAF1D8A, innerGroupElement.DateStart: 03/11/2015 10:45:55
innerGroupElement: System.Data.Entity.DynamicProxies.LockAssignment_F0A35C00917489FA2C4D63DCAECAAD7A1EC03543B62C35206BF0542C5EAF1D8A, innerGroupElement.DateStart: 03/11/2015 10:45:55
这是更新的查询:
var queryNestedGroups =
from lockAssignment in db.LockAssignment
where lockAssignment.UserId == userIdForUsername
let lockAssignmentStartDate = lockAssignment.DateStart
group lockAssignment by new
{
year = lockAssignmentStartDate.Year,
month = lockAssignmentStartDate.Month,
day = lockAssignmentStartDate.Day
} into lockAssignmentsByDateGroup
from lockAssignmentsByZoneGroup in
(from lockAssignment in lockAssignmentsByDateGroup
group lockAssignment by lockAssignment.Lock.Zone.ZoneName)
group lockAssignmentsByZoneGroup by lockAssignmentsByDateGroup.Key;