我在表格中有一些数据如下:
Recipe | Category | Email
我想做的是从源代码中提取这些数据并将其放入类似的内容中:
public class RecipeItem
{
public long Recipe { get; set; }
public long Category { get; set; }
public List<string> Names {get; set; }
}
按配方和类别ID进行分组,并将所有电子邮件放入列表中。
所以,我试过的是做这样的事情:
var recipeItems =
from entry in list
group entry by new { entry.Recipe, entry.Category}
into aRecipe
select new RecipeItem()
{
Recipe = aRecipe.Key.Recipe,
Category = aRecipe.Key.Category,
// ? Not sure how to stick the list of names in here
};
list
是通过实体框架撤回的数据。
但这不太对 - 我想我离这里(也许)。我在这里错过了什么?
随访:
感谢Aducci清除此事。答案是你可以这样做:
Names = aRecipe.Select(x => x.Name)
这会将每个组中的所有名称添加到该组的Names
集合中。非常漂亮。
答案 0 :(得分:2)
我会修改你的课程,看起来像这样
public class RecipeItem
{
public long Recipe { get; set; }
public long Category { get; set; }
public IEnumerable<string> Names {get; set; }
}
您的实体链接查询到:
var recipeItems =
from entry in list
group entry by new { entry.Recipe, entry.Category}
into aRecipe
select new RecipeItem()
{
Recipe = aRecipe.Key.Recipe,
Category = aRecipe.Key.Category,
Names = aRecipe.Select(x => x.Name)
};