Using an IEqualityComparer<T> with group in LINQ
I'm currently trying to run the following code :
void Main()
{
var meta = new LinqMetaData();
var bbSum = (
from tmpar012 in meta.TempUtilityBillingTransactionFile
join bbi in meta.BudgetBilledItem on
tmpar012.TransactionNumber equals bbi.Itxsno
join ms in meta.MeterServices on
bbi.MeterId equals ms.MeterId
join ar201 in meta.ArServiceCodes on
ms.ServiceCodeId equals ar201.Id
where
tmpar012.UpdateNumber == 119985 &&
tmpar012.TransactionCodeId == 153
group i by new TransactionRecordItem {
TransactionCodeId = ar201.Id,
YUsage = bbi.BilledUsage,
YBalance = tmpar012.Amount
} into grouped
select new TransactionRecordItem {
TransactionCodeId = grouped.Key,
YUsage = grouped.Sum(grouped => grouped.Key.BilledUsage),
YBalance = grouped.Sum(ar201 => grouped.Key.Amount)
}
).Dump();
}
// Define other methods and classes here
public class TransactionRecordItem
{
public int TransactionCodeId { get; set;}
public decimal YUsage {get; set;}
public decimal YBalance {get; set;}
}
class TransactionRecordItemEqualityComparer :
IEqualityComparer<TransactionRecordItem>
{
public bool Equals(TransactionRecordItem b1, TransactionRecordItem b2)
{
if (b1.TransactionCodeId == b2.TransactionCodeId)
{
return true;
}
else
{
return false;
}
}
public int GetHashCode(TransactionRecordItem bx)
{
return bx.TransactionCodeId.GetHashCode();
}
}
And I'm getting the following error:
Cannot convert lambda expression to type 'System.Collections.Generic.IEqualityComparer' because it is not a delegate type
On the group
line of the LINQ statement.
What am I missing on this?? Is there some syntax I'm missing for using the IEqualityComparer<T>
with the group
??
UPDATE Ok, I finally figured it out. I was making it more complicated than it needed to be. I did the following and it worked great.
void Main()
{
var meta = new LinqMetaData();
var bbSum2 = (from tmpar012 in meta.TempUtilityBillingTransactionFile
join bbi in meta.BudgetBilledItem on tmpar012.TransactionNumber equals bbi.Itxsno
join ms in meta.MeterServices on bbi.MeterId equals ms.MeterId
join ar201 in meta.ArServiceCodes on ms.ServiceCodeId equals ar201.Id
where tmpar012.UpdateNumber == 119985 && tmpar012.TransactionCodeId == 153
select new TransactionRecordItem {
TransactionCodeId = ar201.Id,
YUsage = bbi.BilledUsage,
YBalance = tmpar012.Amount
}).Dump();
var bbSum3 = (from bbi in bbSum2
group bbi by bbi.TransactionCodeId into grouped
select new TransactionRecordItem {
TransactionCodeId = grouped.Key,
YUsage = grouped.Sum(bbi => bbi.YUsage),
YBalance = grouped.Sum(bbi => bbi.YBalance)
}).Dump();
}
// Define other methods and classes here
public class TransactionRecordItem
{
public int TransactionCodeId 开发者_开发知识库{ get; set;}
public decimal YUsage {get; set;}
public decimal YBalance {get; set;}
}
精彩评论