【问题标题】:Entity Framework query ignoring my orderby实体框架查询忽略我的 orderby
【发布时间】:2014-10-24 11:54:43
【问题描述】:

我自己有这个 SQL 查询

选择 db_accounts_last_contacts.id, dbe_accounts_last_contacts.last_contact_date, db_accounts_last_contacts.description, db_accounts_last_contacts.follow_up_date, db_accounts_last_contacts.spoke_to_person_id, db_accounts_last_contacts.account_idFROM db_accounts_last_contacts , db_companies 在哪里db_companies.id = db_accounts_last_contacts.account_id 通过db_accounts_last_contacts.last_contact_date DESC 订购

返回按 last_contact_date 排序的结果。

现在我有我的实体框架查询

var query = (from c in context.accounts_companies
             select new AccountSearchResultModel()
             {
                 LastContacted = (from calc in context.communique_accounts_last_contacts
                                  where calc.account_id == companyId
                                  orderby calc.last_contact_date descending
                                  select calc.last_contact_date).FirstOrDefault()
            });

但是,当我继续执行我的 ToList 时,我的结果永远不会被排序 这是我未订购的桌子

这是我使用 SQL 查询排序的列表

为什么我的实体框架查询没有提取我的 orderby?或者如果这就是为什么我总是拔出第一个?

【问题讨论】:

  • 然后生成什么SQL?请进行基线调试。提供相关软件的版本号(在本例中为 EF)也可能会有所帮助。
  • TomTom 上面的查询是我的查询的 sn-p,如果我使用调试器将整个查询粘贴到 sql 中以查看 SQL,我的 sql 超过 600 行,所以不确定我是否可以发布我的SQL代码

标签: c# mysql asp.net entity-framework


【解决方案1】:

您需要选择要排序的属性并将其作为 lambda 表达式传递给 OrderByDescending 像这样:

.OrderByDescending(x => x.calc.last_contact_date);

我希望这会有所帮助。

Linq Orderby Descending Query

【讨论】:

  • 我试过LastContacted = (from calc in context.communique_accounts_last_contacts where calc.account_id == companyId //orderby calc.last_contact_date 降序选择 calc.last_contact_date).OrderByDescending(x => x.Value).FirstOrDefault () 但是没有生效
【解决方案2】:

抱歉回复晚了,

最后我要做的是创建一个视图并通过 EDMX 文件导入它,然后使用它来提取我的结果。

【讨论】:

    猜你喜欢
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多