asp.net - How do I check return a value of this object using Linq -


i have 2 related classes:

public class bankholidaycoll {      public list<bankholiday> bankholiday { get; set; } }  public class bankholiday {     public string name { get; set; }     public datetime date { get; set; } } 

i've removed of attributes, object built xml file, this:

var bankholiday = getbankholidaysfromxml(); 

the property bankholiday.bankholiday gives me access list of names , dates (the names being names of bank holidays).

by providing date, want check if list contains date , if return name. i've been trying right combination sometime without success.

thanks in advance.

use firstordefault, example:

public class bankholidaycoll {     public list<bankholiday> bankholiday { get; set; }      public bankholiday getbankholiday(datetime date)     {         if (bankholiday == null || bankholiday.count == 0)             return null;         return bankholiday.firstordefault(h => h.date.date == date.date);     } } 

then null or first instance of bankholiday given date:

bankholiday bh = mybankholidaycoll.getbankholiday(datetime.now); if(bh != null) {     string bhname = bh.name; } 

if insist on method returns name, add this:

public string getbankholidayname(datetime date) {     if (bankholiday == null || bankholiday.count == 0)         return null;     return bankholiday.where(h => h.date.date == date.date)                       .select(h => h.name)                       .firstordefault(); } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -