简体   繁体   English

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

[英]Entity Framework query ignoring my orderby

I have myself this SQL query 我有这个SQL查询

SELECT db_accounts_last_contacts . 选择db_accounts_last_contacts id , dbe_accounts_last_contacts . iddbe_accounts_last_contacts last_contact_date , db_accounts_last_contacts . last_contact_datedb_accounts_last_contacts description , db_accounts_last_contacts . description db_accounts_last_contacts follow_up_date , db_accounts_last_contacts . follow_up_datedb_accounts_last_contacts spoke_to_person_id , db_accounts_last_contacts . spoke_to_person_iddb_accounts_last_contacts account_id FROM db_accounts_last_contacts , db_companies WHERE db_companies . account_id FROM db_accounts_last_contactsdb_companies db_companies id = db_accounts_last_contacts . id = db_accounts_last_contacts account_id ORDER BY db_accounts_last_contacts . account_id ORDER BY db_accounts_last_contacts last_contact_date DESC last_contact_date DESC

Which returns my results ordered by last_contact_date. 它返回我的结果按last_contact_date排序。

Now I have my Entity framework query 现在我有实体框架查询

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()
            });

However when I go ahead and do my ToList on it, my results are never ordered Here is my table un-ordered 但是,当我继续执行ToList时,我的结果从不排序这是我的表未排序 在此处输入图片说明

Here is my list ordered using the SQL query 这是我使用SQL查询排序的列表 在此处输入图片说明

Why isn't my entity framework query not picking up my orderby? 为什么我的实体框架查询没有接我的订单? Or if it is why am I always pulling out the first one? 还是这就是为什么我总是要拔出第一个?

You need to choose a Property to sort by and pass it as a lambda expression to OrderByDescending like this: 您需要选择一个属性进行排序,并将其作为lambda表达式传递给OrderByDescending,如下所示:

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

I hope this helps. 我希望这有帮助。

Linq Orderby Descending Query Linq Orderby降序查询

Sorry for the late answer, 抱歉回复晚了,

What I had to do in the end was create a view and import it via the EDMX file and then use that to pull out my results. 最后,我要做的就是创建一个视图,并通过EDMX文件将其导入,然后使用该视图提取结果。

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

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