c# - 在 foreach 语句中使用 LINQ 从泛型 List<T> 中检索值

标签 c# linq generic-list

我认为这是一个愚蠢(简单)的问题,但我无法找到答案,我可以有效地将 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;
        }
    }

配方类...

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

如果您需要任何更多信息,请告诉我,这是我的第一篇文章,尽管我绞尽脑汁、google 和 stackoverflow,但我还是找不到答案。任何其他从 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;
        }

最佳答案

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

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

关于c# - 在 foreach 语句中使用 LINQ 从泛型 List<T> 中检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40325945/

相关文章:

c# - LINQ to Entities 无法识别方法 'Int32 Next()' 方法

c++ - 通用链表指针访问

java - 如何解决删除节点并转到最后一个节点的问题?

c# - 在 ASP.NET 中重命名并发上传的文件

c# - 拆分列表的元素并仅从拆分中检索第一部分

c# - Entity Framework 的多个条件以获得所需的记录

java - 如何使用 For 循环的每种类型迭代通用列表

C# + 处理 DbConnection 和 DbCommand 并捕获错误

c# - ASPX页面中的脚本标记被部分写入(输出随机停止)

c# - 在 Windows 7 中更改屏幕保护程序设置时应用程序卡住 - System.Threading.Timer 是罪魁祸首?