tags:

views:

59

answers:

2

How would I write a linq to sql statement for the following tsql?

select count(*), sum(Amount), avg(Amount), min(Amount), max(Amount) from TableName

+1  A: 

It's probably easier to pull the values individually but you could do with an anonymous type.

var aggregates = new {
Count = context.TableName.Count(),
Sum = context.TableName.Sum(t => t.Amount),
Avg = context.TableName.Avg(t => t.Amount),
Min = context.TableName.Min(t => t.Amount),
Max = context.TableName.Max(t => t.Amount)
};
Stephan
Want that produce 5 separate queries?
Jeff Langston
You can't do it otherwise in linq, those methods are 'terminators'
argh
+1  A: 

You could do:

var result = new
{
    Count = db.TableName.Count(),
    Sum = db.TableName.Sum(r => r.Amount),
    Average = db.TableName.Avg(r => r.Amount),
    Min = sb.TableName.Min(r => r.Amount),
    Max = db.TableName.Max(r => r.Amount)        
}
Kelsey