繁体   English   中英

实体框架查询忽略我的订单

[英]Entity Framework query ignoring my orderby

我有这个SQL查询

选择db_accounts_last_contacts iddbe_accounts_last_contacts last_contact_datedb_accounts_last_contacts description db_accounts_last_contacts follow_up_datedb_accounts_last_contacts spoke_to_person_iddb_accounts_last_contacts account_id FROM db_accounts_last_contactsdb_companies db_companies id = db_accounts_last_contacts account_id ORDER BY 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查询排序的列表 在此处输入图片说明

为什么我的实体框架查询没有接我的订单? 还是这就是为什么我总是要拔出第一个?

您需要选择一个属性进行排序,并将其作为lambda表达式传递给OrderByDescending,如下所示:

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

我希望这有帮助。

Linq Orderby降序查询

抱歉回复晚了,

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM