亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何設計嵌套查詢以使用實體框架從 MS SQL 表中獲取數據?

如何設計嵌套查詢以使用實體框架從 MS SQL 表中獲取數據?

PHP
慕哥9229398 2024-01-20 16:01:14
我有三個表,您可以在下圖中看到:我的問題是,我想返回所有問題、其響應以及已看到狀態的計數,即尚未看到的響應。我正在使用實體框架。我使用了以下查詢:querySelection = (from problems in db.Problems               join response in db.Response on problems.Id equals response.QueryId                             join order in db.Msg_Orders on response.Id equals order.Response_Id                                           join seen_status in db.Seen_Status on order.Order_Id equals seen_status.OrderId              select new QuerySelect{                 Problem_State = problems.Problem_State,                 Response = response.Response,                 ResponseCount = /*What code should I write here*/               }).ToList();我的上述查詢有兩個問題:期望:它應該只返回獨特的問題和看不見的響應計數得到什么結果:由于 Msg_Orders 有多個問題 ID,并且上述查詢多次返回相同的結果不明白如何在上述查詢中添加未見狀態的計數。
查看完整描述

1 回答

?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

為了實現目標,需要在實體框架語法中使用GROUP BY子句。INNER JOIN

我寫了以下解決方案來解決上述問題:

querySelection = (from problems in db.Problems

? ? ? ? ? ? ? join response in db.Response on problems.id equals response.Query_Id

? ? ? ? ? ? ? join order in db.Msg_Orders on query.id equals order.query_id

? ? ? ? ? ? ? join seen_status in db.Seen_Status on order.Order_id equals seen_status.Order_id

? ? ? ? ? ? ? group new { problems, response, order ,seen_status }

? ? ? ? ? ? ? ? by new

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? problems.Id,

? ? ? ? ? ? ? ? ? ? problems.Problem_State,

? ? ? ? ? ? ? ? ? ? problems.Created_Date,

? ? ? ? ? ? ? ? ? ? response.Response,

? ? ? ? ? ? ? ? ? ? seen_status.User_Seen_Status

? ? ? ? ? ? ? ? } into grp

? ? ? ? ? ? ? orderby grp.Key.Id descending

? ? ? ? ? ? ? select new QuerySelection

? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? Id = grp.Key.Id,

? ? ? ? ? ? ? ? ? Problem_State = grp.Key.Problem_State,

? ? ? ? ? ? ? ? ? Created_Date = grp.Key.Created_Date,

? ? ? ? ? ? ? ? ? Response = grp.Key.Response,

? ? ? ? ? ? ? ? ? TotalResp = grp.Count(x => x.seen_status.user_seen == 0) // Counting total number of responses

? ? ? ? ? ? ? }

? ? ? ? ? ? ? ).ToList();


查看完整回答
反對 回復 2024-01-20
  • 1 回答
  • 0 關注
  • 147 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號