RE: Linq Question
- From: DabblerNL <aner@xxxxxxxxxxxxxxxx>
- Date: Fri, 16 Jan 2009 11:22:07 -0800
var superSaverAccount=(from account in accounts
orderby account.Savings
select account).Last();
"rd" wrote:
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
- Follow-Ups:
- Re: Linq Question
- From: Göran Andersson
- Re: Linq Question
- References:
- Linq Question
- From: rd
- Linq Question
- Prev by Date: Re: autorouting in c#
- Next by Date: Re: autorouting in c#
- Previous by thread: Re: Linq Question
- Next by thread: Re: Linq Question
- Index(es):
Relevant Pages
|