8
select description, min(date), max(date), sum(value1), sum(value2) from table
where description = 'Axxx' and filter = 'L'
group by description

How to perform this query using Linq / C#?

2
  • Have you tried anything so far? Commented Nov 12, 2013 at 13:15
  • Yes, I just can not filter the min and max date. Commented Nov 12, 2013 at 13:19

3 Answers 3

11

Not tested, but the following should work:

table
   .Where(x=>x.description=="Axxx" && x.filter=="L")
   .GroupBy(x=>x.description)
   .Select(x=>new {
       description=x.Key,
       mindate=x.Min(z=>z.date),
       maxdate=x.Max(z=>z.date),
       sumvalue1=x.Sum(z=>z.value1),
       sumvalue2=x.Sum(z=>z.value2)
    });
Sign up to request clarification or add additional context in comments.

Comments

1

something like this should work. not tested

 var q = from b in table
            group b by b.Owner into g
            where description = 'Axxx'
            select new
                       {
                           description = g.description ,
                           date = g.min(),
                           date = g.max(),
                           value1= g.Sum(item => item.value1),
                           value2= g.Sum(item => item.value2),
                       };

Comments

0

That should work

var q = from s in db.table
            where description = 'Axxx' and filter = 'L'
            group s by s.description into g
            select new {YourDescription = g.description, MaxDate = g.Max(s => s.date) }

That answer may help, too .. here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.