Lambda Expression


hi,

i have list<> of records , 1 of property number.

list<record> records = new list<record>();

records.add(new record("low",7);             // 0 - 7 low

records.add(new record("medium",14);      // 8 - 14 medium

records.add(new record("high",21);           // 15 - 21 high

records.add(new record("breached",28);    // 22 above breached

the requirement record out of list based on number.

e.g. 5 low, 20 medium, 30 breached.

i can use loop , status, looking lambda expression or other short , efficient way of doing this.

thanks,

rohit

if expect find 1 only:

class record {     public string text { get; set; }     public int number { get; set; }      public record(string text, int number)     {         text = text;         number = number;     } }  class program {     static void main(string[] args)     {         list<record> records = new list<record>();         records.add(new record("low", 7));          // 0 - 7 low         records.add(new record("medium", 14));      // 8 - 14 medium         records.add(new record("high", 21));        // 15 - 21 high         records.add(new record("breached", 28));    // 22 above breached          var record = records.singleordefault(x => x.number == 21);     } }

if expect find first (if multiple same number), change last line to:

var record = records.firstordefault(x => x.number == 21);

both cases return "null" if number not found.




Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'