2016-06-09 1 views
2

실제로 제목이 혼란스럽고 올바른지 확실하지 않습니다. 여기에 다음과 같은 문제가 있습니다. 각 레시피가 requiredItem [] 배열 (크기 15)을 보유하고있는 목록이 있습니다.이 필수 항목 배열에는 레서피에 필요한 항목을 저장할 수 있습니다. 내가해야 할 일은 모든 요리법을 별도의 목록에 넣는 것입니다. 요리법에서 나무를 필수 항목 중 하나로 사용합니다. 예를 들어, 조리법에 나무 테이블을 만드는 데 10 개의 나무가 필요할 수 있습니다. 이 래서 피는 래서 피 목록 내에 있습니다. 빠른 구글 검색 나 다음을 찾아 만든,하지만 난이 requiredItem 인덱스에 액세스 할 수있는 방법을 알아낼 수 없습니다 :리스트 내의 객체에서 배열의 객체 색인을 가져 오는 Linq 코드

List<Recipe> rec = Main.recipe.ToList(); 
rec.Select((s, i) => new { i, s }) 
.Where(t => t.s.requiredItem[indexhere].type == 9) 
.Select(t => t.i) 
.ToList() 

내가이 보이지 않아요 미안 해요, 난 놀라지 않을 것을 대답은 정말 간단합니다.

나는 또한 다음을 시도했다. 오류는 발생하지 않지만 실제로는 요리법을 선택하지 않습니다.

 Item refItem = new Item(); 
     refItem.SetDefaults(ItemID.Wood, false); 
     List<int> selectWood = new List<int>(rec.Select((r, i) => new { i, r }).Where(x => x.r.requiredItem.ToList().Contains(refItem)).Select(x => x.i).ToList()); 
     ErrorLogger.Log(selectWood.ToArray().ToString()); 
     List<int> indices = new List<int>(); 
     foreach (var indice in selectWood) 
     { 
      for (int i = 0; i < Main.recipe[indice].requiredItem.Length; i++) 
      { 
       var item = Main.recipe[indice].requiredItem[i]; 
       if (item.type == ItemID.Wood && item.stack >= 10) indices.Insert(0, indice); 
      } 
     } 

     foreach (var indice in indices) 
     { 
      ++numberRecipesRemoved; 
      rec.RemoveAt(indice); 
     } 
+0

데이터 예제를 설정하고 반환 할 예제 데이터 항목을 표시하십시오. – OmegaMan

답변

1

나는 질문을 오독 할 수 있지만, 당신이 필요로하고 Any() 절과 같은 지수 (indexHere)이 충분해야하는 이유는 완전히 명확하지 않다.

Recipe 또는 requiredItem의 세부 정보가 없으면 다음 사항을 확인하는 것이 어렵습니다.

enum MaterialType 
{ 
    None, 
    Wood, 
    Glass, 
    Steel, 
    Cloth 
} 

class Ingredient 
{ 

    public Ingredient(MaterialType type, int amount) 
    { 
     Type = type; 
     Amount = amount; 
    } 

    public MaterialType Type { get; } 
    public int Amount { get; } 
} 

class Recipe 
{ 

    public Recipe(string name, params Ingredient[] ingredients) 
     : this(name, (IEnumerable<Ingredient>) ingredients) 
    {   
    } 

    public Recipe(string name, IEnumerable<Ingredient> ingredients) 
    { 
     Name = name; 
     Ingredients = ingredients.ToArray(); 
    } 

    public string Name { get; } 
    public Ingredient[] Ingredients { get; } 
} 

[TestClass] 
public class FindAllItemsFixture 
{ 

    private readonly static IEnumerable<Recipe> AllItemRecipes = new List<Recipe> 
    { 
     new Recipe("Sword", new Ingredient(MaterialType.Steel, 3)), 
     new Recipe("Spear", new Ingredient(MaterialType.Steel, 1), new Ingredient(MaterialType.Wood, 3)), 
     new Recipe("Table", new Ingredient(MaterialType.Wood, 6)), 
     new Recipe("Chair", new Ingredient(MaterialType.Wood, 4)), 
     new Recipe("Flag", new Ingredient(MaterialType.Cloth, 2)), 

    }; 

    IEnumerable<Recipe> GetAllRecipesUsingMaterial(MaterialType materialType) 
    { 
     return AllItemRecipes.Where(r => r.Ingredients.Any(i => i.Type == materialType)); 
    } 


    [TestMethod] 
    public void GetAllWoodenRecipes() 
    { 
     var expectedNames = new string[] { "Spear", "Table", "Chair" }; 
     var woodenItems = GetAllRecipesUsingMaterial(MaterialType.Wood); 
     CollectionAssert.AreEqual(expectedNames, woodenItems.Select(i => i.Name).ToArray()); 

    } 

    [TestMethod] 
    public void GetAllClothRecipes() 
    { 
     var expectedNames = new string[] { "Flag" }; 
     var clothItems = GetAllRecipesUsingMaterial(MaterialType.Cloth); 
     CollectionAssert.AreEqual(expectedNames, clothItems.Select(i => i.Name).ToArray()); 

    } 
} 

중요한 부분은 단위 테스트에서 GetAllRecipesUsingMaterial() 함수이다. 지정된 유형의 재료를 포함하는 모든 래서 피를 선택합니다.

+0

Woow .. 너무 길게 프로그래밍하는 고전적인 예. 이 명확한 예는 매우 간단합니다. 다음 코드를 사용하여 원하는 것을 구현할 수있었습니다. numberRecipesRemoved + = rec.RemoveAll (x => x.requiredItem.Any (i => i.type == ItemID.Wood && i.stack> = 10)); 이 코드는 현재 내가 원하는 것을하고 있습니다. 정말 고마워요 : D (GetAllRecipesUsingMaterial 예제도 정말 도움이됩니다) – Jofairden

1

예, 간단합니다. 질문을 이해하면 나무가 들어있는 모든 리셉션을 필수 항목으로 표시해야합니다. 그렇게 하시겠습니까?

var woodRecipes = recipes.Where(r => r.requiredItems.Contains(wood)).ToList(); 
관련 문제