Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.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/1/list/4.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# 简单C列表:给定主键,如何在列表中查找和更新嵌套对象属性?_C#_List - Fatal编程技术网

C# 简单C列表:给定主键,如何在列表中查找和更新嵌套对象属性?

C# 简单C列表:给定主键,如何在列表中查找和更新嵌套对象属性?,c#,list,C#,List,简单的IList更新问题:给定以下嵌套对象,如何在给定主键的情况下更新嵌套最深的属性 public class Recipe { public int RecipeID {get;set;} // PK public string name {get;set;} public IList<RecipeStep> RecipeSteps {get;set;} } public class RecipeStep { public int Rec

简单的IList更新问题:给定以下嵌套对象,如何在给定主键的情况下更新嵌套最深的属性

public class Recipe {
    public int RecipeID {get;set;}     // PK

    public string name {get;set;}
    public IList<RecipeStep> RecipeSteps {get;set;}
}

public class RecipeStep {
    public int RecipeID {get;set;}     // PK
    public int RecipeStepID {get;set;} // PK

    public string name {get;set;}
    public IList<Ingredient> {get;set;}
}

public class Ingredient {
    public int RecipeID {get;set;}     // PK
    public int RecipeStepID {get;set;} // PK
    public int IngredientID {get;set;} // PK

    public string name {get;set;}
}

那么,如果RecipeID=2、RecipeTepId=14和IngredientID=5是int的值,而不是索引,那么如何设置Recipe.RecipeStep.component.name呢。希望有一些直接的方法来引用这些项目,而不需要循环。Linq表达式很好。当我尝试Linq时,我最终更改了值的一个副本,而不是值本身。LOL.

您正在寻找selectmany,它可以获取ienumerable的多个深度,并将它们平铺成一个统一的结果集,在一个ienumerable中,一个selectmany可以获取所有成分,然后根据您的条件做一个where

Ingredient ing = theRecipe.RecipeSteps.SelectMany((recipeStep) => recipeStep.Ingredient)
    .FirstOrDefault((ingredient) =>
        ingredient.RecipeId == 2 &&
        ingredient.RecipeStepId == 14 &&
        ingredient.IngredientId == 5);

ing.name = "pretty flowers";
Linq查询:

Ingredient ing = theRecipe.RecipeSteps.Ingredients
    .FirstOrDefault(i =>
        i.RecipeId == 2 &&
        i.RecipeStepId == 14 &&
        i.IngredientId == 5);

if (ing != null) ing.name = "New Name";

如果这是一个复制粘贴,你会在RecipeStep中丢失你的IList成员的名称。这不起作用,因为配料是RecipeStep列表中的一个列表。我已经更正了这个问题。。。但你是对的——你的代码关心很多关系。所有这些都是很好的答案,但这一个相当时髦。我担心它不会返回引用值,因此更新referent.name将更新原始文件而不是副本。效果不错。我还喜欢select语句from、where、from、where的嵌套特性。谢谢。在LinqToObjects中,编织不仅仅是为了展示,这种技术可以防止查询探索额外的receipes和recipestep。
Ingredient theIngredient =
(
  from r in Recipes
  where r.RecipeId == 2
  from rs in r.RecipeSteps
  where rs.RecipeStepID == 14
  from ing in rs.Ingredients
  where ing.IngredientId == 5
  select ing
).Single()

theIngredient.name = theName;