RE: Linq Question

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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



.



Relevant Pages

  • [NEWS] Serious Vulnerabilities Found in Rediffmail.com Web Mail Service (CSS)
    ... has been found to not properly filter out HTML and JavaScript ... var sessionid, login, ifs, ife, link, version; ... 'oldpassdb' contains the encoded/encrypted form of the ... a Rediff.com account with the following user ids: ...
    (Securiteam)
  • Re: Implicit object constructor misinterpretation
    ... into the source text (or token stream) in order to account for ... And indeed such insertions would be ... var x = { ...
    (comp.lang.javascript)
  • Re: Linq Question
    ... Suppose I have the below collection of account object and I want to select a ... single account with the largest savings. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Linq Question
    ... Suppose I have the below collection of account object and I want to select a single account with the largest savings. ...
    (microsoft.public.dotnet.languages.csharp)
  • Linq Question
    ... Suppose I have the below collection of account object and I want to select a ... single account with the largest savings. ...
    (microsoft.public.dotnet.languages.csharp)