Reputation: 2974
I have the below, which has worked fine until I added the last part:
var FbtTotals = from ff in ExpenseItemFbtItems
group ff by ff.ERLineID into g
select new {
lineId = g.Key, totalAttendees = g.Sum(
m => m.Num_Attendees_In_Group),
attendeeTypes = g.Count(),
purposeDesc = g.FirstOrDefault(n => n.User_Purpose_Description)
};
The purposeDesc fails, saying it is an unknown error. Functionally, I just want to do the same thing I did with the 'sum' of totalAttendees - but I just want the first element rather than an aggregation.
What have I overlooked?
Upvotes: 3
Views: 111
Reputation: 185703
I'm assuming you're using an ORM in conjunction with LINQ (EF, NHibernate, LINQ to SQL, etc.). The ORM's don't support windowing functions on sets (which is what would be required in order to translate your query); they only support aggregates.
In order to do what you want, you will have to retrieve all of the records in your query then construct your final object (getting the first item) in client code. Given that you don't seem to be filtering on anything, you can try this:
var FbtTotals = from ff in ExpenseItemFbtItems.AsEnumerable()
group ff by ff.ERLineID into g
select new { lineId = g.Key, totalAttendees = g.Sum(m => m.Num_Attendees_In_Group), attendeeTypes = g.Count(), purposeDesc = g.FirstOrDefault(n => n.User_Purpose_Description) };
Upvotes: 1