我有以下代码:
var checkRegisters = await Database.Client.Context.CheckRegister
.Where(cr => cr.CheckingAccount == account
&& cr.CheckDate >= fromDate
&& cr.CheckDate <= toDate
&& cr.Void == "N" )
.GroupBy(cr => cr.CheckNumber,
cr => cr.Amount, (key, g) =>
new {
CheckNumber = key,
Amount = g.Sum()
})
.OrderBy(c => c.CheckNumber)
.ToListAsync();
除了向我返回CheckNumber和Amount的列表之外,我还不太了解它在做什么。 CheckRegister表包含Payee列,EF中的相应modelBuilder也包含:
entity.Property(e => e.Payee).HasColumnType("varchar(255)");
如何将收款人添加到返回的列表中?
答案 0 :(得分:2)
代码的作用:
您的数据库有一个包含CheckRegisters
的表。每个CheckRegister
至少具有属性CheckingAccount
,CheckDate
,Void
,CheckNumber
和Amount
,根据您的情况,它也具有属性{ {1}}:
因此,您的代码将逐步介绍。
Payee
现在您想要的更改:添加// Take the complete table of CheckRegisters:
Database.Client.Context.CheckRegister
// from every checkRegister in this table (every row),
// keep only those checkRegisters that have a CheckingAccount value equal to account
// and a CheckDate value betwen fromData and toDate
// and a value for property Void equalt to "N"
.Where(checkRegister => checkRegister.CheckingAccount == account
&& checkRegister.CheckDate >= fromDate
&& checkRegister.CheckDate <= toDate
&& checkRegister.Void == "N" )
// Group all remaining CheckRegisters into groups with same checkNumber
// Each group will have a property Key which has the value of the common CheckNumber
.GroupBy(cr => cr.CheckNumber,
// ElementSelector: from every CheckRegister put only the amount in each grouop
checkRegister => checkRegister.Amount,
// ResultSelector: take every key (which we call checkNumber)
// and all Amounts of all CheckRegisters with this CheckNumber
// to make one new object:
(checkNumber, amountsFromCheckRegistersWithThisCheckNumber) => new
{
CheckNumber = key,
// To calculate property Amount: sum all amountsFromCheckRegistersWithThisCheckNumber
Amount = amountsFromCheckRegistersWithThisCheckNumber.Sum(),
})
// By now you have a sequence of objects, each with two properties:
// CheckNumber: a checkNumber used in the CheckRegisters that were left after the Where
// Amount: the sum of all Amounts of all CheckRegisters that have this CheckNumber
// Finally you do some Ordering, and convert the resulting elements into a list
.OrderBy(groupedItem => groupedItem.CheckNumber)
.ToListAsync();
。
问题是,返回的数据是按CheckNumber分组的CheckRegisters。每个收款人都是每个CheckRegister。如果将具有相同CheckNumber的CheckRegisters组组成,则可以使用此CheckNumber获得几位收款人:
Payees
如果我使用相同的CheckNumber进行分组,则会得到:
CheckNumber | Payee | ...
1 | A
2 | B
1 | C
2 | C // note: C is used again
1 | D
因此,每个组可能有不止一名员工。如果可以的话,只需省略ElementSelector并在ResultSelector中选择雇员
group CheckNumber 1 [1, A], [1,C], [1,D]
group CheckNumber 2 [2, B], [2,C]
答案 1 :(得分:1)
我会将其添加到您在GroupBy中创建的对象中。因此,如下更改第3行
.GroupBy(cr => cr.CheckNumber, cr => cr.Amount, cr => cr.Payee, (key, g) =>
new { CheckNumber = key, Amount = g.Sum(), Payee = g.Payee})
请注意,分组依据只是按数字和金额分开,因此,如果您也按收款人分组,则应该预期结果列表会更长。这也假设收款人是表中列的名称。
答案 2 :(得分:0)
通过以下方式将新列添加到组中:
var checkRegisters = await Database.Client.Context.CheckRegister
.Where(cr => cr.CheckingAccount == account && cr.CheckDate >= fromDate && cr.CheckDate <= toDate && cr.Void == "N" )
.GroupBy(cr => cr.CheckNumber, cr => cr.Amount, e => e.Payee , (key, g, p) => new { CheckNumber = key, Amount = g.Sum(), Payee = p})
.OrderBy(c => c.CheckNumber).ToListAsync();