R
rd
Hi,
Suppose I have the below collection of account object and I want to select a
single account with the largest savings. Putting aside the fact that the
below would cause an exception if there were more than one account with
Savings = 40, is this a good way to search the collection? Is there a better
way?
var accounts = new []
{
new {Savings = 20, Owner = "Mary"},
new {Savings = 30, Owner = "Jack"},
new {Savings = 40, Owner = "Bill"}
};
var account = accounts.Single(y => y.Savings == (accounts.Max(x =>
x.Savings)));
Console.Write(account.Owner); // Bill
Regards,
Rich
Suppose I have the below collection of account object and I want to select a
single account with the largest savings. Putting aside the fact that the
below would cause an exception if there were more than one account with
Savings = 40, is this a good way to search the collection? Is there a better
way?
var accounts = new []
{
new {Savings = 20, Owner = "Mary"},
new {Savings = 30, Owner = "Jack"},
new {Savings = 40, Owner = "Bill"}
};
var account = accounts.Single(y => y.Savings == (accounts.Max(x =>
x.Savings)));
Console.Write(account.Owner); // Bill
Regards,
Rich