我有以下表格,每個客戶都有多個跟單
。

想檢索customerslist其中Followups.attendedDate > customer.attendedDate和where [most recent for customer] Followup.statusid=2 or 7 or 8 or 9
我做的代碼
Dim Result As List(Of Customer) = Await DB. 客戶.OrderByDescending(Function(x) x.AttendedDate) _
.Include(Function(x) x.FollowUps.Select(Function(y) y.CallStatu)) _
.Where(Function(x) x.AttendedBy IsNot Nothing Andalso)
x.FollowUps.Any(Function(z) z.StatusID = 2 Or z。 StatusID = 7 Or z.StatusID = 8 Or) _
.Where(Function(x) x.FollowUps。 Where(Function(y) y.AttendedDate > x.AttendedDate).Count > 0) .ToListAsync
''要洗掉除2、7、8、9以外的狀態,要在名為removelist的串列中添加條目。
Dim RemoveList As New List(Of Customer)
'查找每個客戶沒有最后狀態值2,7,8,9的條目。
For Each cust As Customer In Result
If cust.FollowUps.Last.StatusID <> 2 And cust。 FollowUps.Last.StatusID <> 7 And cust. FollowUps.Last.StatusID <> 8 cust.FollowUps.Last.StatusID <> 9 Then
RemoveList.Add(cust)
End If
下一步 下一步
'從原始結果中洗掉條目
For Each Cust As Customer In RemoveList
結果.洗掉(Cust)
下一步
查詢給出customerlist,其中customer where statusID = 2|7|8|9匹配。但是我只想讓客戶有一個最新的statusID=2|7|8|9。
因此,在給定的樣本資料中,客戶Steve應該在結果串列中,因為他的最后狀態是followup(2)。客戶John不應該在串列中,因為它沒有跟進。客戶Mark也不應該在結果串列中,因為在他的所有狀態中,他最后/最新的狀態是NotInterested(1)
。TIA
uj5u.com熱心網友回復:
從FollowUps開始并回傳到客戶可能更簡單。這樣就隱含地洗掉了FollowUps為零的客戶。
Dim stats = { 2, 7,8, 9 }
db.FollowUps.Include(Function(fu) fu.Customer) _
.GroupBy(Function(fu) fu.CustomerId) _
.Select(Function(g) g.OrderByDescending(Function(fu) fu.AttendedDate) .First() _
.Where(Function(fu) stats.Contains(fu.StatusId) AndAlso fu.AttendedDate > fu.Customer.AttendedDate) _
.Select(Function(fu) fu.Customer)
也許可以通過將attendeddates的where移到前面來使include隱含起來
。db.FollowUps _
.Where(Function(fu) fu.AttendedDate > fu.Customer.AttendedDate) _
.GroupBy(Function(fu) fu.CustomerId) _
.Select(Function(g) g.OrderByDescending(Function(fu) fu.AttendedDate) .First() _
.Where(Function(fu) stats.Contains(fu.StatusId)) _
.Select(Function(fu) fu.Customer)
我相當肯定EF可以翻譯這個模式......但現在還沒有能力測驗。如果你得到一個 "查詢不能被翻譯",請發表評論
。uj5u.com熱心網友回復:
運行EF查詢后
db.FollowUps _
.Where(Function(fu) fu.AttendedDate > fu.Customer.AttendedDate) _
.GroupBy(Function(fu) fu.CustomerId) _
.Select(Function(g) g.OrderByDescending(Function(fu) fu.AttendedDate) .First() _
.Where(Function(fu) stats.Contains(fu.StatusId)) _
.Select(Function(fu) fu.Customer)
當我運行下面的代碼來檢查檢索的值時
For Each cust As CustomerIn result2
Debug.WriteLine(cust.Firstname & " "/span> & cust.Lastname)
For Each fw As FollowUp In cust.FollowUps
Debug.WriteLine(fw.CallStatu.Status & vbTab & fw.AttendedDate)
下一步
Debug.WriteLine("---------------------")
下一步
結果我得到
。Chirag Panchal
檔案拾取 17/09/2021 12:02:03 PM
檔案拾取 17/09/2021 12:02:20 PM
檔案拾取 17/09/2021 12:04:33 PM
檔案取回 17/09/2021 12:06:48 PM
收到的檔案 17/09/2021 12:26:33 PM
后續行動 13/09/2021 5:06:28 PM
感興趣的 13/09/2021 5:07:02 PM
有興趣 01/09/2021 11:56:38 AM
"---------------------"
Ravibhai Prajapati
檔案拾起 01/09/2021 11:56:45 AM
在這里,日期似乎是以降序排列的,但每條記錄的各自時間是以升序排列的
。轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/334302.html
標籤:
