按名称添加新的变量,如果我使用分组:中的LINQ查询
List<Recipe> Recipes = Show.GroupBy(t => t.Name)
.Select(g => new Recipe() { Name = g.Key, components = g.Select(t => new Component {Amount= t.Amount, Ingredient= t.Ingredient })
.ToList() })
.ToList();
它写在Name
价值g.Key
,但如果我想在LINQ DishID
添加新的变量如何写有从DisplayRecipe
DishID
?
List<Recipe> Recipes = Show.GroupBy(t => t.Name)
.Select(g => new Recipe() { Name = g.Key,DishID= ???, components = g.Select(t => new Component {Amount= t.Amount, Ingredient= t.Ingredient })
.ToList() })
.ToList();
型号:
public class DisplayRecipe
{
public string Name { get; set; }
public string Ingredient { get; set; }
public string Amount {get;set;}
public int DishID { get; set; }
}
public class Recipe
{
public string Name { get; set; }
public int DishID { get; set; }
public List<Component> components {get;set;}
}
public class Component
{
public string Ingredient { get; set; }
public string Amount { get; set; }
}
如果两个'Recipe'对象具有相同的'Name'但不同的'DishID'会怎么样?你想使用哪种'DishID'?或者你想用“DishID”来分组吗? –
什么是“显示”类型? –