我試圖讓這個LINQ工作,但失敗的錯誤。與GroupBy LINQ錯誤
無法轉換lambda表達式鍵入「System.Collections.Generic.IEqualityComparer」,因爲它不是一個委託類型
基本上我有IEnumerable<DataRow>
和我想要對數據進行分組,如:
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<string>("WorkPhone").Substring(0, 3),
Contact = sr.Field<string>("Contact") },
(key, lst) => new Customer
{
Id = Guid.NewGuid(),
AreaCode = key.AreaCode,
CustAccount = key.Contact,
FirstPhoneNo = lst.First().Field<string>("WorkPhone").Substring(0, 10),
FirstSRNum= lst.First().Field<string>("SRNum"),
SRCount = lst.Count()
})
.Take(5);
有什麼想法?
DigEmAll的建議幫助(謝謝),我不得不這樣做:
public class GroupKey
{
public string AreaCode { get; set; }
public string Contact { get; set; }
}
,然後更改LINQ到這個GroupBy
鍵:
GroupBy<DataRow, GroupKey, IEnumerable<Customer>>
return siebelRows.GroupBy<DataRow, GroupKey, IEnumerable<Customer>>(sr => new GroupKey
{
Contact = sr.Field<string>("Contact"),
AreaCode = sr.Field<string>("WorkPhone").Substring(0, 3)
},
(key, lst) => new Customer
{
Id = Guid.NewGuid(),
AreaCode = key.AreaCode,
CustAccount = key.Contact,
FirstPhoneNo = lst.First().Field<string>("WorkPhone").Substring(0, 10),
FirstSRNum = lst.First().Field<string>("SRNum"),
SRCount = lst.Count()
}).OrderByDescending(c => c.SRCount)
.Take(5);
不喜歡創造一個關鍵的具體類型......任何方式圍繞?
好像LINQ正在尋找第二個過載一個需要的IEqualityComparer公共靜態的IEnumerable>的GroupBy ( 此IEnumerable的源, Func鍵的KeySelector, 的IEqualityComparer 比較器); –
Sunit
2010-10-04 19:02:30