【问题标题】:EntityFramework group by linq queryEntityFramework group by linq 查询
【发布时间】:2015-02-16 14:44:40
【问题描述】:

我有以下 2 个表:

Account
----------
account_id **PK**
account_name
account_type_id **FK** 

Account_Type
-------------
account_type_id **PK (FK in account table)**
account_type_name
account_type_reference

使用实体框架我试图得到这样的结果:

DbSet.Where(Account => Account.Account_Type.account_type_name == 'type1').ToList());

这可行,但是我不确定如何将 group by 添加到此语句中。我想按 Account_Type_id、Account_type_name、Account_type_reference 对这些进行分组,这样我就不会得到重复的行。我该怎么做?

谢谢

【问题讨论】:

  • account_type_id 分组就足够了,因为它是独一无二的(我希望如此)。

标签: c# linq linq-to-entities entity-framework-6


【解决方案1】:

您可以使用 Linq GroupBy 函数添加分组:

DbSet.Where(Account => Account.Account_Type.account_type_name == "type1")
    .GroupBy(x => x.Account_Type.account_type_id).ToList();

然后你会得到一个组列表。然后每个组都有 Key (account_type_id),并且组本身是 Account 对象的 IEnumerable

【讨论】:

    猜你喜欢
    • 2021-09-23
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    相关资源
    最近更新 更多