在 foreach 语句中使用 LINQ 从通用列表 <T> 中检索值

Retrieve a value from a generic List<T> using LINQ in a foreach statement

我认为这是一个愚蠢(简单)的问题,但我一直无法找到答案,我可以有效地使用 LINQ XML(这是我的 LINQ 体验的全部)但是当我尝试从通用列表或列表(包含在描述中)中获取字符串形式的实际值,我只得到类似“{System.Linq.Enumerable.WhereSelectListIterator}”的东西,我希望它说 "scrambled-eggs.jpg"

private static List<Recipe> currentRecipeList = new List<Recipe>();
public static List<Recipe> CurrentRecipeList { get { return currentRecipeList; } set { currentRecipeList = value; } }//Populated from an XML document 

public static string GetSpecificRecipeValue(string recipeName, int index, int ingredientIteration = -1, int ingredientValue = -1)//From CurrentRecipeList get value(Index) where recipeName is equal to CurrentRecipeList.RecipeName.
    {
        IEnumerable<string> recipeElement = null;
        IEnumerable<string> ingredientElement = null;
        if (index == 0)
        { recipeElement = (from el in currentRecipeList where recipeName == el.RecipeName select el.RecipeType); }
        else if (index == 1)
        { recipeElement = (from el in currentRecipeList where recipeName == el.RecipeName select el.RecipeName ); }
        else if (index == 2)
        { recipeElement = (from el in currentRecipeList where recipeName == el.RecipeName select el.RecipeSource); }
        else if (index == 3)
        { recipeElement = (from el in currentRecipeList where recipeName == el.RecipeName select el.RecipeID); }
        else if (index == 4)
        { recipeElement = (from el in currentRecipeList where recipeName == el.RecipeName select el.RecipePicture); }
        else if (index == 5)
        { recipeElement = (from el in currentRecipeList where recipeName == el.RecipeName select el.RecipeDescription); }
        else if (index == 6)
        { recipeElement = (from el in currentRecipeList where recipeName == el.RecipeName select el.RecipeMethod); }
        else if (index == 7)
        { recipeElement = (from el in currentRecipeList where recipeName == el.RecipeName select el.RecipeCost); }
        else if (index == 8)
        { recipeElement = (from el in currentRecipeList where recipeName == el.RecipeName select el.RecipeDifficulty); }
        else if (index == 9)
        { recipeElement = (from el in currentRecipeList where recipeName == el.RecipeName select el.RecipeServings); }
        else if (index == 10)
        { recipeElement = (from el in currentRecipeList where recipeName == el.RecipeName select el.RecipePreparationTime); }
        else if (index == 11)
        { recipeElement = (from el in currentRecipeList where recipeName == el.RecipeName select el.RecipeCookingTime); }
        else if (index == 12)
        { recipeElement = (from el in currentRecipeList where recipeName == el.RecipeName select el.RecipeGlobalRating); }
        else if (index == 13)
        { recipeElement = (from el in currentRecipeList where recipeName == el.RecipeName select el.RecipeUserRating); }
        else if (index == 14)
        { recipeElement = (from el in currentRecipeList where el.RecipeName == recipeName select el.RecipeTags); }
        else if (index == 15 && ingredientValue == 0)
        { ingredientElement = (from el in currentRecipeList where el.RecipeName == recipeName select el.RecipeIngredients[ingredientIteration].Item); }
        else if (index == 15 && ingredientValue == 1)
        { ingredientElement = (from el in currentRecipeList where recipeName == el.RecipeName select el.RecipeIngredients[ingredientIteration].Quantity); }
        else if (index == 15 && ingredientValue == 2)
        { ingredientElement = (from el in currentRecipeList where recipeName == el.RecipeName select el.RecipeIngredients[ingredientIteration].Unit.ToString()); }
        else if (index == 15 && ingredientValue == 3)
        { ingredientElement = (from el in currentRecipeList where recipeName == el.RecipeName select el.RecipeIngredients[ingredientIteration].State); }
        else if (index == 15 && ingredientValue == 4)
        { ingredientElement = (from el in currentRecipeList where recipeName == el.RecipeName select el.RecipeIngredients[ingredientIteration].Type); }

        else
        { recipeElement = null; ingredientElement = null; }


        if (recipeElement != null)
        {
            return recipeElement.ToString();
        }
        else if (ingredientElement != null)
        {
            return ingredientElement.ToString();
        }
        else
        {
            return null;
        }
    }

食谱Class...

public class Recipe
{

    public string RecipeType { get; set; }
    public string RecipeName { get; set; }
    public string RecipeSource { get; set; }
    public string RecipeID { get; set; }
    public string RecipePicture { get; set; }//File name of Picture to read from pictures folder
    public string RecipeDescription { get; set; }//ShortDescription 
    public string RecipeMethod { get; set; }
    public string RecipeCost { get; set; }
    public string RecipeDifficulty { get; set; }
    public string RecipeServings { get; set; }
    public string RecipePreparationTime { get; set; }
    public string RecipeCookingTime { get; set; }
    public string RecipeGlobalRating { get; set; }
    public string RecipeUserRating { get; set; }
    public string RecipeTags { get; set; }
    public List<Ingredient> RecipeIngredients { get; set; }

}

成分Class..

public class Ingredient
{
    public string Item { get; set; }
    public string Quantity { get; set; }
    public string Unit { get; set; }
    public string State { get; set; }
    public string Type { get; set; }
}

如果您需要更多信息,请告诉我,这是我的第一个 post,虽然我绞尽脑汁,google 和 Whosebug,但我还是找不到答案。任何其他从 RecipeName 获取特定信息作为参数或我可以自己学习此信息的来源的方法,将不胜感激。

编辑 好的,感谢 BJ Myers,这是作为返回结果的正确解决方案。

private static List<Recipe> currentRecipeList = new List<Recipe>();
public static List<Recipe> CurrentRecipeList { get { return currentRecipeList; } set { currentRecipeList = value; } }//Populated from an XML document.
public static string GetSpecificRecipeValue(string recipeName, int index, int ingredientIteration = -1, int ingredientValue = -1)//From CurrentRecipeList get value(Index) where recipeName is equal to CurrentRecipeList.RecipeName.
{
    IEnumerable<string> recipeElement = null;
    IEnumerable<string> ingredientElement = null;
    if (index == 0)
    { recipeElement = (from el in currentRecipeList where recipeName == el.RecipeName select el.RecipeType); }
    else if (index == 1)
...

if (recipeElement != null)
        {
            string result = recipeElement.FirstOrDefault<string>().ToString();
            return result;
        }
        else if (ingredientElement != null )
        {
            return ingredientElement.FirstOrDefault<string>().ToString();
        }
        else
        {
            return null;
        }

答案:.FirstOrDefault来自 returns 所需的值而不是实现 IEnumerable 的 class。

string result = recipeElement.FirstOrDefault<string>().ToString();
        return result;

Thomas 提供的答案是使用 .FirstOrDefault()。但是,只是为了帮助您编写代码,我想我会向您展示一种更短的方法来完成您正在做的事情。试试这个:

public static string GetSpecificRecipeValue(string recipeName, int index, int ingredientIteration = -1, int ingredientValue = -1)//From CurrentRecipeList get value(Index) where recipeName is equal to CurrentRecipeList.RecipeName.
{
    Func<Recipe, string>[] properties = new Func<Recipe, string>[]
    {
        el => el.RecipeType,
        el => el.RecipeName,
        el => el.RecipeSource,
        el => el.RecipeID,
        el => el.RecipePicture,
        el => el.RecipeDescription,
        el => el.RecipeMethod,
        el => el.RecipeCost,
        el => el.RecipeDifficulty,
        el => el.RecipeServings,
        el => el.RecipePreparationTime,
        el => el.RecipeCookingTime,
        el => el.RecipeGlobalRating,
        el => el.RecipeUserRating,
        el => el.RecipeTags,
    };

    Func<Recipe, string>[] ingredients = new Func<Recipe, string>[]
    {
        el => el.RecipeIngredients[ingredientIteration].Item,
        el => el.RecipeIngredients[ingredientIteration].Quantity,
        el => el.RecipeIngredients[ingredientIteration].Unit,
        el => el.RecipeIngredients[ingredientIteration].State,
        el => el.RecipeIngredients[ingredientIteration].Type,
    };

    return
        currentRecipeList
            .Where(el => recipeName == el.RecipeName)
            .Select(el =>
                index < 15
                    ? properties[index](el)
                    : ingredients[ingredientValue](el))
            .FirstOrDefault();
}