这个LINQ查询可以简化吗?

时间:2011-08-10 12:17:42

标签: .net linq entity-framework

来自Java,我是C#和LINQ的新手。我们的代码库中有许多查询似乎没有最佳构造。在以下查询中,GroupBy lambda表达式创建匿名数据类型。我在网上找不到像这样使用GroupBy的任何例子。有没有办法简化此查询并仍然返回相同的结果?

 List<MachineMetrics> machines = prod.Where(p => p.TimeProduced >= start &&
                                      p.TimeProduced <= end &&
                                      (jobID == -1 ? true : (p.JobID == jobID && p.Job.MachineID == MachineID))).
                                GroupBy(x => new
                                             {
                                                 MachineName = x.Job.Machine.MachineName,
                                                 MachineID = x.Job.MachineID,
                                                 JobName = x.Job.JobName,
                                                 JobID = x.JobID
                                              }).
                                Select(item => new MachineMetrics()
                                              {
                                                 MachineName = item.Key.MachineName,
                                                 MachineID = item.Key.MachineID,
                                                 JobName = item.Key.JobName,
                                                 JobID = item.Key.JobID
                                              }).
                                ToList<MachineMetrics>();

编辑:感谢您的帮助。问题是Equals()和GetHashCode()方法没有对该类进行实施。一旦我添加了那些我使用了@Ladislav Mrnka建议的代码,一切都按预期工作。

2 个答案:

答案 0 :(得分:4)

您正在寻找:

List<MachineMetrics> machines = prod.Where(p => p.TimeProduced >= start &&
                                                p.TimeProduced <= end &&
                                                (jobID == -1 || 
                                                    (p.JobID == jobID && p.Job.MachineID == MachineID))).
                                    .Select(x => new MachineMetrics()
                                          {
                                             MachineName = x.Job.Machine.MachineName,
                                             MachineID = x.Job.MachineID,
                                             JobName = x.Job.JobName,
                                             JobID = x.JobID
                                          })
                                    .Distinct()
                                    .ToList();

答案 1 :(得分:2)

拉迪斯拉夫的回答很好,但只是为了展示另一种选择,保留GroupBy,你可以将其缩小为:

var machines = prod.Where(p => p.TimeProduced >= start &&
                               p.TimeProduced <= end &&
                               (jobID == -1 ? true : (p.JobID == jobID && p.Job.MachineID == MachineID))).
                    GroupBy(x => new MachineMetrics
                                 {
                                     MachineName = x.Job.Machine.MachineName,
                                     MachineID = x.Job.MachineID,
                                     JobName = x.Job.JobName,
                                     JobID = x.JobID
                                  }).
                    Select(item => item.Key). // 'item' is the grouping, and its 'Key' is the 'MachineMetrics' instance
                    ToList();