by Naveen
25. March 2011 04:03
If you have a collection of class object and you want to extract an array or list of only one of the properties, lot of time we end up writing code that iterates over each item and then putting it in a new array or list. There is a clean and quick way of doing it using Linq. Following code snippet shows how I extracted an array of ingredient names from a List of Ingredient class objects.
public string[] SearchIngredient(string prefixText, int count)
{
var ingredientsList = IngredientData.GetIngredientsWithNameLike(prefixText);
if (null == ingredientsList ||
ingredientsList.Count == 0)
{return null;}
var nameQuery = from ingredient in ingredientsList
select ingredient.Name;
return nameQuery.ToArray();
}
25325589-aa07-4f39-9379-d535489f604d|0|.0
Views: 1589
Tags: Linq
LINQ
Sale Coupons
Deals Of The Day