user215675
user215675

Reputation: 5181

C# Linq queries

How to find Min,Max,Sum of given array?

int[] number = { 1, 2, 3, 78, 100, 1001 };

(not working)

var query = new { maximum = number.Max, 
minimum = number.Min, Sum = number.Sum }; 

Upvotes: 0

Views: 148

Answers (3)

barkimedes
barkimedes

Reputation: 341

Max, Min, and Sum are methods, not properties of the number array. So, you have to call them as you would a method (with parentheses).

var query = new { maximum = number.Max(), minimum = number.Min(), Sum = number.Sum() };

Upvotes: 0

Zenon
Zenon

Reputation: 1456

those are functions, so

number.Max()

number.Min()

number.Sum()

Upvotes: 1

eglasius
eglasius

Reputation: 36027

You can:

var values = new { 
               maximum = number.Max(), 
               minimum = number.Min(), 
               Sum = number.Sum() 
            };

Note that those are 3 separate calls, like if it were linq2sql, those would cause 3 separate roundtrips. To pull it off in a single roundtrip, you could have a query that gives a single element in the from x in y where somecondition select ...

Upvotes: 3

Related Questions