Linq to SQL Grouping

D

David

Hi all,

At the moment, I am not sure how I would apply this in SQL yet, so I don't
know if it is possible immediately without making it too complicated.

I am running Linq to SQL.

I have this query.

var TrailerHistory = from th in dc.TrailerHistories
where th.TrailerID == FleetIDEdit.Text
&& th.DateOfRepair >=
DateTime.Today.AddYears(-1)
group th by th.Grp into g
select new
{
grp = g.Key,
cost = g.Sum(a => a.Value)
};

This works fine, however, the client has now asked that I have both the cost
within the past year (as the query returns) and cost for the whole history
(not limited to the past year)

I have absolutely no idea how I can achieve this without using a second
similar query without the time constraint.

Any ideas that I can follow would be very much appreciated.

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
 
A

Ashish Sheth

Can you try:

var TrailerHistory = from th in dc.TrailerHistories
group th by th.Grp into g
select new
{
grp = g.Key,
wholeHistorycost = g.Sum(a => a.Value),
pastOneYearCost = g.Where(b =>
b.DateOfRepair >= DateTime.Today.AddYears(-1)).Sum(c => c.Value)
};
I guess this should work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

linq and gridview sorting 2
How do I do this in Linq 3
Linq 3
Linq without DBML 2
Selecting a variable value with Linq 5
Left join with Linq to Sql 6
Linq problem 7
Looping through Linq fields 5

Top