从泛型列表中检索值<在foreach语句中使用LINQ

本文关键字:语句 foreach LINQ 泛型 列表 检索 | 更新日期: 2023-09-27 18:15:35

我认为这是一个愚蠢的(简单的)问题,但我没能找到答案,我可以有效地使用LINQ的XML(这是我的LINQ经验的全部),但当我试图从一个通用列表或列表(包括在描述)中获得实际值作为字符串时,我只得到像"{System.Linq.Enumerable。whereselectlisttiterator}"我想在这里写"scramble -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;
        }
    }

Recipe 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; }
}

配料类…

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; }
}

请让我知道如果你需要更多的信息,这是我的第一个帖子,虽然我已经绞尽脑汁,谷歌和stackoverflow,答案逃避我。从RecipeName中获取特定信息的任何其他方法,作为参数或来源,我可以自己学习这些信息,将非常感谢。

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

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;
        }

从泛型列表中检索值<在foreach语句中使用LINQ

答案:.FirstOrDefault from返回所需值,而不是实现IEnumerable的类。

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();
}