我试图让这个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);
不喜欢为钥匙创建一个具体的类型......任何方式?