Tuesday, 15 June 2010

asp.net mvc - Linq, how to Group by Field, Sum on Field, First on Field -



asp.net mvc - Linq, how to Group by Field, Sum on Field, First on Field -

hi i'm having little problem following

i have table (callrecords) navigation property table (resultcodes)

i want perform groupby (callrecords) on resultcodesid sum (the occurrences of resultcodesid)

first on included table , field resultcodes.name, i.e name of resultcode (via navigation property)

var exceptions = db.callrecords .include(x => x.resultcode) .where(x => x.clientid == id && x.resultcodeid < 0 && x.starttime >= startdate && x.starttime <= finishdate) .groupby(o => o.resultcodeid) .select(g => new exceptionviewmodel { code = g.key ?? 0, total = g.count(), name = g.first(x => x.resultcode.name) });

this problem, next line wont compile

name = g.first(x => x.resultcode.name)

cannot convert look type 'string' homecoming type bool

the reply (seemingly) simple, google , stack searches given me except examples need, thought reply might help other unwary travelers

update

additional info

view model

public class exceptionviewmodel { public int code { get; set; } public int total { get; set; } public string name { get; set; } }

data

public class resultcode { [required,key] public int code { get; set; } public string name { get; set; } public string description { get; set; } } public class callrecord { [required] public int id { get; set; } // other properties removed brevity [required] public int? resultcodeid { get; set; } public virtual resultcode resultcode { get; set; } }

as can see properties involved problem look above of type string, im not sure whether i'm having brain-fart or there dont understand

the look g.first(x => x.resultcode.name) not think does. when first has argument argument supposed predicate filters enumerable sequence.

in other words, .first(x => ...) equivalent .where(x => ...).first(). if @ way it's clear x.resultcode.name not valid in context.

what want first item in each grouping , fish out info it. so:

g.first().resultcode.name

asp.net-mvc group-by linq-to-entities

No comments:

Post a Comment