在我的项目中,
我有两张桌子
(1)Import_detail table (Date, Description,Parti_Name, Company_name,Amount )
和
(2)Export_detail table (Date, Description,Parti_Name, Company_name,Amount )
我希望使用linq在DataGrid中将此详细信息显示为表格详细信息。 在这里我使用工会加入合并专栏,但我不知道如何保持平衡
作为这张表。
我正在使用linq lambda Expression。 请帮帮我。
此处代码为联合表
var query = dm.Import_detail.Select(r => r).Union( dm.Export_detail
.Select.Select(q => q));
答案 0 :(得分:1)
看看这个例子。可能是你想要的。 我有3个班级
public class Import
{
public DateTime date { get; set; }
public decimal amount { get; set; }
}
public class Export
{
public DateTime date { get; set; }
public decimal amount { get; set; }
}
public class Result
{
public DateTime date { get; set; }
public decimal creditAmount { get; set; }
public decimal debitAmount { get; set; }
public decimal balanceAmount { get; set; }
}
选择
var importList = new List<Import>()
{
new Import{date = DateTime.Parse("2009-01-02"), amount = 10000},
new Import{date = DateTime.Parse("2009-01-25"), amount = 6000}
};
var exportList = new List<Export>()
{
new Export{date = DateTime.Parse("2009-01-05"), amount = 500},
new Export{date = DateTime.Parse("2009-01-10"), amount = 1000},
new Export{date = DateTime.Parse("2009-01-11"), amount = 1500},
new Export{date = DateTime.Parse("2009-01-15"), amount = 4000},
new Export{date = DateTime.Parse("2009-01-28"), amount = 5000}
};
var temp = importList.Select(c => new Result { date = c.date, debitAmount = c.amount, creditAmount = 0, balanceAmount = 0 }).Union(
exportList.Select(c => new Result { date = c.date, debitAmount = 0, creditAmount = c.amount, balanceAmount = 0 })).GroupBy(c => c.date).Select(c => new Result { date = c.Key, creditAmount = c.Sum(g => g.creditAmount), debitAmount = c.Sum(g => g.debitAmount) }).OrderBy(c => c.date).ToList();
foreach (var item in temp)
item.balanceAmount = temp.Where(c => c.date < item.date).OrderByDescending(c => c.date).Select(c => c.balanceAmount).FirstOrDefault() + item.debitAmount - item.creditAmount;
结果:
debitAmount creditAmount balanceAmount
10000 0 10000
0 500 9500
0 1000 8500
0 1500 7000
0 4000 3000
6000 0 9000
0 5000 4000