2016-10-30 2 views
1

これは愚かな(単純な)質問だと思うが、回答を見つけることができず、XML用にLINQを効果的に使用することができます私のLINQの経験の))しかし、私は実際の値を一般的なリストまたはリスト(説明に含まれている)から文字列として取得しようとすると、私は "{System.Linq.Enumerable.WhereSelectListIterator}" "スクランブル-eggs.jpg"foreach文でLINQを使用して汎用リスト<T>から値を取得する

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

'where'節を使うと、要素の* collection *を返します。 (実際には 'WhereSelectListIterator'のような' IEnumerable'を実装したクラスを返します。一つの項目が必要なら '.First'または' .FirstOrDefault'を使う必要があります。 –

+0

華麗な、ありがとう、 –

+0

補足として、C#は文字列だけでなく、組み込みのデータ型も異なっています。 –

答えて

2

回答:戻るから.FirstOrDefault必要な値ではなく、IEnumerableを実装するクラス。

1

答えは、Thomasによって提供されるように、.FirstOrDefault()です。しかし、あなたのコードを手助けするだけで、私はあなたにあなたがやっていることをやるためのより短い方法を示したいと思っていました。これを試してみてください:

+0

このクラスを完成し、残りのコードの動作を知っていただきありがとうございます。私はあなたのソリューションを使用するつもりですが、私はそれがどのように動作するのかまだ分かりません。 –

+0

@SirajMansour - いいえ、そうではありません。 'Func 'のどれかが実行された場合にのみ失敗します。初期化の間、それらはそうではありません。 – Enigmativity

関連する問題