加入&;使用LINQ转换多个列表
本文关键字:转换 列表 LINQ 使用 amp 加入 | 更新日期: 2023-09-27 18:22:09
Hi我有下面的代码,它会返回正确的数据,但似乎必须有更好的方法来组合3个列表,基于它们的公共字段,并使用LINQ将结果转换为给定类型的新列表,而不是在最后使用foreach。有什么想法吗?
public IEnumerable<StagSummaryByCflHistoricalItem> GetSummaryByCflHistorical(DateTime currentDate)
{
var allRecords =
this.preGrantSummaryHistoricalRepository
.AllWithFetch(this.preGrantSummaryHistoricalRepository.All, x => x.CaseFileLocation)
.Where(
x => x.Date >= currentDate.FirstDayOfQuarterFromDateTime()
&& x.Date <= currentDate.LastDayOfQuarterFromDateTime())
.ToList();
var summaryForQuarter =
allRecords.GroupBy(x => new { x.CaseFileLocation.Id, x.CaseFileLocation.Name }).Select(
x =>
new
{
CaseFileLocationId = x.Key.Id,
Description = x.Key.Name,
TotalCasesEnteredCfl = x.Sum(y => y.TotalCasesEntered),
TotalNetFeeEnteredCfl = x.Sum(y => y.TotalNetFeeEntered),
TotalCasesLeftCfl = x.Sum(y => y.TotalCasesLeft),
TotalNetFeeLeftCfl = x.Sum(y => y.TotalNetFeeLeft)
})
.OrderBy(x => x.CaseFileLocationId)
.ToList();
var summaryForMonth =
allRecords.Where(x => x.Date >= currentDate.FirstDayOfMonthFromDateTime())
.GroupBy(x => new { x.CaseFileLocation.Id, x.CaseFileLocation.Name }).Select(
x =>
new
{
CaseFileLocationId = x.Key.Id,
Description = x.Key.Name,
TotalCasesEnteredCfl = x.Sum(y => y.TotalCasesEntered),
TotalNetFeeEnteredCfl = x.Sum(y => y.TotalNetFeeEntered),
TotalCasesLeftCfl = x.Sum(y => y.TotalCasesLeft),
TotalNetFeeLeftCfl = x.Sum(y => y.TotalNetFeeLeft)
})
.OrderBy(x => x.CaseFileLocationId)
.ToList();
var summaryForWeek =
allRecords.Where(x => x.Date >= currentDate.FirstDayOfWeekFromDateTime(DayOfWeek.Monday)).GroupBy(
x => new { x.CaseFileLocation.Id, x.CaseFileLocation.Name }).Select(
x =>
new
{
CaseFileLocationId = x.Key.Id,
Description = x.Key.Name,
TotalCasesEnteredCfl = x.Sum(y => y.TotalCasesEntered),
TotalNetFeeEnteredCfl = x.Sum(y => y.TotalNetFeeEntered),
TotalCasesLeftCfl = x.Sum(y => y.TotalCasesLeft),
TotalNetFeeLeftCfl = x.Sum(y => y.TotalNetFeeLeft)
})
.OrderBy(x => x.CaseFileLocationId)
.ToList();
var finalList = summaryForQuarter
.Select(x => new StagSummaryByCflHistoricalItem()
{
CaseFileLocationId = x.CaseFileLocationId,
Description = x.Description,
QuarterTotalCasesEnteredCfl = x.TotalCasesEnteredCfl,
QuarterTotalCasesLeftCfl = x.TotalCasesLeftCfl,
QuarterTotalNetFeeEnteredCfl = x.TotalNetFeeEnteredCfl,
QuarterTotalNetFeeLeftCfl = x.TotalNetFeeLeftCfl
})
.OrderBy(x => x.CaseFileLocationId)
.ToList();
foreach (var qrt in finalList)
{
var mnthData = summaryForMonth.FirstOrDefault(x => x.CaseFileLocationId == qrt.CaseFileLocationId);
if (mnthData != null)
{
qrt.MonthTotalCasesEnteredCfl = mnthData.TotalCasesEnteredCfl;
qrt.MonthTotalCasesLeftCfl = mnthData.TotalCasesLeftCfl;
qrt.MonthTotalNetFeeEnteredCfl = mnthData.TotalNetFeeEnteredCfl;
qrt.MonthTotalNetFeeLeftCfl = mnthData.TotalNetFeeLeftCfl;
}
var weekData = summaryForWeek.FirstOrDefault(x => x.CaseFileLocationId == qrt.CaseFileLocationId);
if (weekData == null)
{
continue;
}
qrt.WeekTotalCasesEnteredCfl = weekData.TotalCasesEnteredCfl;
qrt.WeekTotalCasesLeftCfl = weekData.TotalCasesLeftCfl;
qrt.WeekTotalNetFeeEnteredCfl = weekData.TotalNetFeeEnteredCfl;
qrt.WeekTotalNetFeeLeftCfl = weekData.TotalNetFeeLeftCfl;
}
return finalList;
}
注意:我有意先将整个季度的数据作为一个列表,然后对其进行操作,以完成本月的工作;季度总数,但这主要是因为我无法从LINQ IQuerable组合中找到最终结果。
我使用的是NHibernate、LINQ方法语法、存储库模式和SQL Server 2008
如果我正确阅读了您的代码,那么您将结果投影到具有相同结果成员的匿名类型,这使得将结果连接在一起非常容易。我最近在Union做了类似的事情。下面是我刚刚写的一个简化示例:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace ConsoleApplication1 {
public class Month {
public int MonthID { get; set; }
public string MonthName { get; set; }
public int NoDays { get; set; }
}
internal class Program {
private static void Main(string[] args) {
// Build up months
var months = new List<Month>();
for (var i = 1; i <= 12; i++) {
months.Add(new Month {
MonthID = i,
MonthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(i),
NoDays = DateTime.DaysInMonth(2012, i)
});
}
var w = months.Select(m => new {
m.MonthName
});
var x = months.Select(m => new {
m.MonthName
});
var y = months.Select(m => new {
m.MonthName
});
var z = w.Union(x).Union(y);
foreach (var m in z) {
Console.WriteLine(m.MonthName);
}
Console.Read();
}
}
}
请记住,"联合"(类似于SQLUNION子句)将从列表中删除任何重复项。如果您不想删除重复项(即执行"union all"),请按如下方式使用"Concat":
var z = w.Concat(x).Concat(y);