C# lambda GroupJoin 但按另一列分组
C# lambda GroupJoin but Group by another column
所以,我正在用 ASP.NET Entity Framework 制作一个小程序。这些是相关模型:
public class Tops
{
public Tops()
{
Dresses = new HashSet<Dresses>();
}
[Key] public int id { get; set; }
public string name { get; set; }
public int material { get; set; }
public int meter { get; set; }
public Inventory materialNavigation { get; set; }
public Parts_material PartNavigation { get; set; }
public ICollection<Dresses> Dresses { get; set; }
}
public class Parts_material
{
public Parts_material()
{
Tops = new HashSet<Tops>();
Sleeves = new HashSet<Sleeves>();
Patterns = new HashSet<Patterns>();
}
public int part_id { get; set; }
public int material_id { get; set; }
public Material MaterialNavigation { get; set; }
public ICollection<Tops> Tops { get; set; }
public ICollection<Sleeves> Sleeves { get; set; }
public ICollection<Patterns> Patterns { get; set; }
}
public class Material
{
public Material()
{
Parts_Materials = new HashSet<Parts_material>();
Inventories = new HashSet<Inventory>();
}
public int material_id { get; set; }
public string material_name { get; set; }
public ICollection<Parts_material> Parts_Materials { get; set; }
public ICollection<Inventory> Inventories { get; set; }
}
如您所见,Tops
和 Material
之间存在多对多的关系。如果我想创建一个新的 Top,我想 select 使用 SelectList 的材料。到目前为止,我的 lambda 查询是这样的:
var parts_Materials = _context.Parts_Material.GroupJoin(_context.Materials,
parts => parts.material_id,
material => material.material_id,
(parts, material) => new { Parts_material = parts.part_id, Material = material })
.Select(e => new {
material_code = e.Material.Select(a => a.material_id),
material_name = String.Join(", ", e.Material.Select(a => a.material_name))
})
.ToList();
ViewData["material"] = new SelectList(parts_Materials, "material_code", "material_name");
问题是,如果我没有理解错的话,它会按 Parts_material.material_id
分组,但我希望它按 Parts_material.part_id
分组。
我已经为此苦苦挣扎了几天,所以我希望有人能帮助我。
已更新 因为 Tops.material
实际上是 Parts_material.part_id
.
来自评论的示例数据的预期输出:
<select name="part">
<option value="1">Kant</option>
<option value="2">Kant, Jersey</option>
</select>
加入后需要单独分组,不能一次性加入
ViewData["part"] = new SelectList(
from pm in _context.Parts_Material
join m in _context.Materials on pm.material_id equals m.material_id
group m.material_name by pm.part_id into names
select new
{
part_id = names.Key,
material_name = string.Join(", ", names)
}
, "part_id", "material_name");
group
行表示按部件 ID 将 material 名称分组为 IGrouping<>
,将部件 ID 映射到 IEnumerable<>
个 material 名称。然后将每个组扁平化为匿名类型,其值和文本内容为 <option>
.
我建议您将 material
重命名为 part
以避免将来混淆。
所以,我正在用 ASP.NET Entity Framework 制作一个小程序。这些是相关模型:
public class Tops
{
public Tops()
{
Dresses = new HashSet<Dresses>();
}
[Key] public int id { get; set; }
public string name { get; set; }
public int material { get; set; }
public int meter { get; set; }
public Inventory materialNavigation { get; set; }
public Parts_material PartNavigation { get; set; }
public ICollection<Dresses> Dresses { get; set; }
}
public class Parts_material
{
public Parts_material()
{
Tops = new HashSet<Tops>();
Sleeves = new HashSet<Sleeves>();
Patterns = new HashSet<Patterns>();
}
public int part_id { get; set; }
public int material_id { get; set; }
public Material MaterialNavigation { get; set; }
public ICollection<Tops> Tops { get; set; }
public ICollection<Sleeves> Sleeves { get; set; }
public ICollection<Patterns> Patterns { get; set; }
}
public class Material
{
public Material()
{
Parts_Materials = new HashSet<Parts_material>();
Inventories = new HashSet<Inventory>();
}
public int material_id { get; set; }
public string material_name { get; set; }
public ICollection<Parts_material> Parts_Materials { get; set; }
public ICollection<Inventory> Inventories { get; set; }
}
如您所见,Tops
和 Material
之间存在多对多的关系。如果我想创建一个新的 Top,我想 select 使用 SelectList 的材料。到目前为止,我的 lambda 查询是这样的:
var parts_Materials = _context.Parts_Material.GroupJoin(_context.Materials,
parts => parts.material_id,
material => material.material_id,
(parts, material) => new { Parts_material = parts.part_id, Material = material })
.Select(e => new {
material_code = e.Material.Select(a => a.material_id),
material_name = String.Join(", ", e.Material.Select(a => a.material_name))
})
.ToList();
ViewData["material"] = new SelectList(parts_Materials, "material_code", "material_name");
问题是,如果我没有理解错的话,它会按 Parts_material.material_id
分组,但我希望它按 Parts_material.part_id
分组。
我已经为此苦苦挣扎了几天,所以我希望有人能帮助我。
已更新 因为 Tops.material
实际上是 Parts_material.part_id
.
来自评论的示例数据的预期输出:
<select name="part">
<option value="1">Kant</option>
<option value="2">Kant, Jersey</option>
</select>
加入后需要单独分组,不能一次性加入
ViewData["part"] = new SelectList(
from pm in _context.Parts_Material
join m in _context.Materials on pm.material_id equals m.material_id
group m.material_name by pm.part_id into names
select new
{
part_id = names.Key,
material_name = string.Join(", ", names)
}
, "part_id", "material_name");
group
行表示按部件 ID 将 material 名称分组为 IGrouping<>
,将部件 ID 映射到 IEnumerable<>
个 material 名称。然后将每个组扁平化为匿名类型,其值和文本内容为 <option>
.
我建议您将 material
重命名为 part
以避免将来混淆。