当前位置:  开发笔记 > 编程语言 > 正文

GroupBy的LINQ错误

如何解决《GroupBy的LINQ错误》经验,为你挑选了0个好方法。

我试图让这个LINQ工作,但失败了错误.

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

基本上我有IEnumerable,我正在尝试对数据进行分组,如:

string sql = @"SELECT [t0].[Contact_Account] AS [Contact], [t0].[Work_Phone] AS [WorkPhone], [t0].[SR_NUM] AS [SRNum] ";
sql += "FROM [Base_SR] AS [t0] ";
sql += "WHERE ([t0].[Country] = 'USA') AND (NOT ([t0].[Work_Phone] LIKE '+%')) ";
sql += "AND ([t0].[Product] = 'SP3D') AND (DATEPART(Year, [t0].[DateOpened]) = {0})";

sql = String.Format(sql, curYear);

var sqlCmd  = new SqlCommand(sql, new SqlConnection(connectionString));
var adapter = new SqlDataAdapter(sqlCmd);
var dataSet = new DataSet();
adapter.Fill(dataSet);
var siebelRows = dataSet.Tables[0].AsEnumerable();

return siebelRows.GroupBy(sr => new { AreaCode = sr.Field("WorkPhone").Substring(0, 3), 
          Contact = sr.Field("Contact") },
     (key, lst) => new Customer 
     {
                        Id = Guid.NewGuid(),
      AreaCode = key.AreaCode,
      CustAccount = key.Contact,
      FirstPhoneNo = lst.First().Field("WorkPhone").Substring(0, 10),
      FirstSRNum= lst.First().Field("SRNum"),
      SRCount = lst.Count()
     })
     .Take(5);

有什么想法吗 ?

DigEmAll的建议有所帮助(谢谢),我不得不这样做:

public class GroupKey
{
    public string AreaCode { get; set; }
    public string Contact { get; set; }
}

然后将LINQ更改为此GroupBy键:

GroupBy>

return siebelRows.GroupBy>(sr => new GroupKey
{
    Contact = sr.Field("Contact"),
    AreaCode = sr.Field("WorkPhone").Substring(0, 3)
},
    (key, lst) => new Customer
        {
            Id = Guid.NewGuid(),
            AreaCode = key.AreaCode,
            CustAccount = key.Contact,
            FirstPhoneNo = lst.First().Field("WorkPhone").Substring(0, 10),
            FirstSRNum = lst.First().Field("SRNum"),
            SRCount = lst.Count()
        }).OrderByDescending(c => c.SRCount)
        .Take(5);

不喜欢为钥匙创建一个具体的类型......任何方式?

推荐阅读
手机用户2402852387
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有