Friday, June 05, 2009

Filter one list using another list in LINQ

public class Country
{
public string CountryID { get; set; }
public string CountryVal { get; set; }
public string CountryDesc { get; set; }


}



List<Country> sourceCountry = new List<Country>
{
new Country{ CountryID="1" , CountryVal="IND", CountryDesc="India"},
new Country{ CountryID="2" , CountryVal="USA", CountryDesc="America"},
new Country{ CountryID="3" , CountryVal="UK", CountryDesc="London"},
new Country{ CountryID="4" , CountryVal="PAR", CountryDesc="Paris"}
};

List<Country> filterCountry = new List<Country>
{
new Country{ CountryDesc="India"},
new Country{ CountryDesc="London"}

};


var finalcountry = from item in sourceCountry
from some in filterCountry
where item.CountryDesc == some.CountryDesc
select item;

List<Country> fin = finalcountry.ToList<Country>();

No comments: