I'm trying to get this LINQ to work but fails with the error.
Cannot convert lambda expression to type 'System.Collections.Generic.IEqualityComparer' because it is not a delegate type
Basically I have IEnumerable and I'm trying to group the data, as in:
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);
Any thoughts ?
DigEmAll's suggestion helped (thanks) and I had to do this:
public class GroupKey
{
public string AreaCode { get; set; }
public string Contact { get; set; }
}
And then change the LINQ to this GroupBy key:
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);
Don't like creating a concrete type for the key...any way around this ??
thanks Sunit