C# LINQ 生成List<T>交叉表数据
《费用一览表》报表
LINQ 源码
C# 全选
/// <summary>
/// 费用一览表报表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public res_AccountFeeSummary QueryAccountFeeSummary(req_AccountFee input)
{
var q = _Database.GetQueryable<dt_AccountFee>();
var q_AccountItems = _Database.GetQueryable<dt_AccountItems>();
q = q.Where(w => w.FlagApp == "Y");
if (!input.FENO.IsEmpty())
q = q.Where(w => w.FENO == input.FENO);
if (input.DocDateFrom > Globals.MinSqlDate)
q = q.Where(w => w.DocDate >= input.DocDateFrom);
if (input.DocDateTo > Globals.MinSqlDate)
q = q.Where(w => w.DocDate <= input.DocDateTo);
if (!input.AccountId.IsEmpty())
q = q.Where(w => w.AccountId == input.AccountId);
if (!input.ARAP.IsEmpty())
q = q.Where(w => w.ARAP == input.ARAP);
if (!input.SONO.IsEmpty())
q = q.Where(w => w.SONO == input.SONO);
//左连接,left join
var listSource = (from a in q
join b in q_AccountItems on a.AccountId equals b.ItemID into tmp
from b in tmp.DefaultIfEmpty()
select new
{
isid = a.isid,
YYYYMM = a.DocDate.Value.Year.ToString() + a.DocDate.Value.Month.ToString().PadLeft(2, '0'),
ItemId = a.AccountId,
ItemName = b != null ? b.ItemName : "",
Amount = a.ARAP == "收" ? a.Amount : (a.Amount ?? 0) * -1,//收:正数,支:负数
}).ToList();
//01/02/03项目编码
var stringList = listSource.Select(s => s.ItemId).Distinct().ToList();
//返回的结果数据
var listResult = new List<res_AccountFeeSummaryItem>();
//设置交叉表数据
foreach (var item in listSource)
{
var row = listResult.Where(s => s.YYYYMM == item.YYYYMM).FirstOrDefault();
if (row == null)
{
row = new res_AccountFeeSummaryItem
{
YYYYMM = item.YYYYMM,
ItemId = item.ItemId,
ItemName = item.ItemName,
};
listResult.Add(row);
}
//生成属性名称:F01/F02/F03
var fieldName = "F" + (stringList.IndexOf(item.ItemId) + 1).ToString().PadLeft(2, '0');
//动态赋值
ObjectHelper.SetPropertyValue(row, fieldName, (row.Amount ?? 0) + (item.Amount ?? 0));
}
//返回结果
return new res_AccountFeeSummary
{
AccountItems = stringList,
ListData = listResult,
};
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
CSFramework.EF C/S框架网