Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从常规列表中检索值<;T>;在foreach语句中使用LINQ_C#_Linq_Generic List - Fatal编程技术网

C# 从常规列表中检索值<;T>;在foreach语句中使用LINQ

C# 从常规列表中检索值<;T>;在foreach语句中使用LINQ,c#,linq,generic-list,C#,Linq,Generic List,我假设这是一个愚蠢(简单)的问题,但我还没有找到答案,我可以将LINQ有效地用于XML(这是我的全部LINQ经验),但当我试图从通用列表或列表(包括在描述中)中获取字符串形式的实际值时,我只得到类似“{System.LINQ.Enumerable.WhereSelectListIterator}”的内容我想说“炒蛋.jpg” 如果你需要更多信息,请告诉我,这是我的第一篇帖子,尽管我一直在绞尽脑汁,谷歌和stackoverflow,但我还是找不到答案。如果能通过任何其他方法从收件人处获取一条特定信

我假设这是一个愚蠢(简单)的问题,但我还没有找到答案,我可以将LINQ有效地用于XML(这是我的全部LINQ经验),但当我试图从通用列表或列表(包括在描述中)中获取字符串形式的实际值时,我只得到类似“{System.LINQ.Enumerable.WhereSelectListIterator}”的内容我想说“炒蛋.jpg”

如果你需要更多信息,请告诉我,这是我的第一篇帖子,尽管我一直在绞尽脑汁,谷歌和stackoverflow,但我还是找不到答案。如果能通过任何其他方法从收件人处获取一条特定信息,作为我自己能够了解这些信息的论点或来源,我将不胜感激

编辑 好的,这是正确的解决方案,这是由于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;
        }
private static List currentRecipeList=new List();
公共静态列表CurrentRecipeList{get{return CurrentRecipeList;}set{CurrentRecipeList=value;}}//从XML文档填充。
公共静态字符串GetSpecificRecipeValue(字符串recipeName,int-index,int-IngReditentiation=-1,int-IngRediteValue=-1)//从CurrentRecipeList获取值(索引),其中recipeName等于CurrentRecipeList.recipeName。
{
IEnumerable RecipeeElement=null;
IEnumerable ingredientElement=null;
如果(索引==0)
{recipeElement=(从currentRecipeList中的el开始,其中recipeName==el.recipeName选择el.RecipeType);}
else if(索引==1)
...
if(recipeElement!=null)
{
字符串结果=recipeElement.FirstOrDefault().ToString();
返回结果;
}
如果(ingredientElement!=null),则为else
{
返回ingredientElement.FirstOrDefault().ToString();
}
其他的
{
返回null;
}

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

string result = recipeElement.FirstOrDefault<string>().ToString();
        return result;
string result=recipeElement.FirstOrDefault().ToString();
返回结果;

托马斯提供的答案是使用
.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();
}
public静态字符串GetSpecificRecipeValue(字符串recipeName,int-index,int-IngRediteration=-1,int-IngRediteValue=-1)//从CurrentRecipeList获取值(index),其中recipeName等于CurrentRecipeList.recipeName。
{
Func[]属性=新Func[]
{
el=>el.RecipeType,
el=>el.RecipeName,
el=>el,
el=>el.RecipeID,
el=>el.RecipePicture,
el=>el.RecipeDescription,
el=>el.RecipeMethod,
el=>el.RecipeCost,
el=>el.交互困难,
el=>el.RecipeServings,
el=>el.RecipePreparationTime,
el=>el.RecipeCokingTime,
el=>el.RecipeGlobalRating,
el=>el.RecipeU锯齿形,
el=>el.RecipeTags,
};
Func[]成分=新Func[]
{
el=>el.ReceivingElements[IngCredition]。项,
el=>el.ReceivingElements[IngCredition]。数量,
el=>el.ReceivingElements[IngCredition]。单位,
el=>el.RecipeIngElements[IngRediteration]。状态,
el=>el.RecipeIngElements[IngRediteration]。类型,
};
返回
当前收件人
.Where(el=>recipeName==el.recipeName)
.选择(el=>
指数<15
?属性[索引](el)
:成分[IngCreditValue](el))
.FirstOrDefault();
}

使用
where
子句返回元素的集合,而不仅仅是一个。(它实际上返回了一个实现了
IEnumerable
的类,比如
WhereSelectListIterator
。如果你想要一个项目,你需要使用
。First
。FirstOrDefault
。谢谢你,我会在几分钟内给你荣誉和post结果。顺便说一句,C有不同的内置数据类型,而不是on你来自javascript环境吗?:)@DiligentKeyPresser是的,我确实是从Java开始的,但我只使用过基本数组之类的。谢谢你,在我完成了这个课程并知道我其余的代码工作后,我将使用你的解决方案,部分原因是我还不知道它是如何工作的。@SirajMansour-不,它没有。只有当
Func
中的任何一个被禁用时,它才会失败已执行。在初始化期间,它们不会被执行。
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;
        }
string result = recipeElement.FirstOrDefault<string>().ToString();
        return result;
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();
}