English 中文(简体)
简单C# List<>:鉴于主要关键因素,如何在清单中找到和更新被冻结的物体财产?
原标题:Simple C# List<>: given the primary key, how to find and update a nested object property in a List<>?
  • 时间:2010-07-26 19:34:49
  •  标签:
  • c#
  • list

简明扼要的问题:鉴于以下特有物体,鉴于主要关键因素,你如何更新最深的特有财产?

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

因此,我如何确定收款人。 RecipeStep.Ingredient.name given that RecipeID = 2, RecipeStepID = 14, and IngredientID = 5 ( which are the Value of the int, not the index). 希望能有一些直接的办法,在没有住所的情况下提及这些物品。 Linq 言论是罚款。 (当我尝试Linq时,我最后修改了价值的副本,而不是价值本身。) LOL。

最佳回答
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;
问题回答

你们重新寻找选择,可以采取多种深度的计算方法,将其整理成一个可计算数的、能够获取所有成分的单一组合结果,然后在什么地方()满足你们的条件。

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

ing.name = "pretty flowers";

Linq query:

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

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签