在LINQ </t>中使用IEqualityComparer <t>和group

时间:2011-10-06 13:35:01

标签: .net linq

我目前正在尝试运行以下代码:

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();
    }
}

我收到以下错误:

  

无法将lambda表达式转换为type   'System.Collections.Generic.IEqualityComparer'   因为它不是委托类型

在LINQ语句的group行上。

我错过了什么?使用带有IEqualityComparer<T> ??

group时是否缺少某些语法

更新 好吧,我终于明白了。我让它变得比它需要的更复杂。我做了以下工作,效果很好。

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;}
}

0 个答案:

没有答案