簡體   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