I have the following LINQ query:
var q = from bal in data.balanceDetails
where bal.userName == userName && bal.AccountID == accountNumber
select new
{
date = bal.month + "/" + bal.year,
commission = bal.commission,
rebate = bal.rebateBeforeService,
income = bal.commission - bal.rebateBeforeService
};
I remember seeing a lambda shorthand for summing the commission field for each row of q.
What would be the best way of summing this? Aside from manually looping through the results?
It’s easy – no need to loop within your code:
Note that if you’re going to use the results of
qfor various different calculations (which seems a reasonable assumption, as if you only wanted the total commission I doubt that you’d be selecting the other bits) you may want to materialize the query once so that it doesn’t need to do all the filtering and projecting multiple times. One way of doing this would be to use:That will create a
List<T>for your anonymous type – you can still use theSumcode above onresultshere.