![](/img/trans.png)
[英]How to add conditions to update an entry with entity framework linq-to-entities
[英]LINQ-to-Entities query on Entity Framework
我很難創建滿足我要求的 LINQ-to-Entities 查詢。
我有兩個表: Booking
和ProcessStatusLog
。
預訂表:
ProcessStatusLog表:
以下是這些表的示例數據:
預訂表
| PNNumber | Account Name |
+----------+----------------+
| 11111 | Boston Celtics |
| 22222 | Miami Heat |
| 33333 | LA Lakers |
ProcessStatusLog表:
| ID | PNNumber | InsuranceCode | Status | UpdatedOn |
+------+-----------+---------------+--------------+-------------+
| 1 | 11111 | null | NEW | 02/22/2020 |
| 2 | 11111 | FIRE | FOR REVIEW | 02/23/2020 |
| 3 | 22222 | null | NEW | 02/24/2020 |
| 4 | 22222 | MORTGAGE | FOR REVIEW | 02/25/2020 |
| 5 | 22222 | MORTGAGE | CORRECTION | 02/26/2020 |
| 6 | 22222 | FIRE | FOR REVIEW | 02/27/2020 |
| 7 | 33333 | null | NEW | 02/28/2020 |
| 8 | 22222 | FIRE | APPROVED | 02/29/2020 |
現在,我想獲得每個最新狀態的預訂列表。
例如:如果我想過濾最新狀態為“CORRECTION”的預訂,我將獲得 PNNumber 為 22222 的預訂。
如果搜索“FOR REVIEW”,我只會獲得 PNNumber 11111 的預訂。
如果搜索“NEW”,我只會獲得 PNNumber 33333 的預訂。
我如何為此編寫 EF 查詢?
謝謝。
context.ProcessStatusLog.where(x=>x.Status == "FOR REVIEW")
.OrderByDescending(x => x.UpdatedOn)
.Take(1);
您可以使用Join()
來實現它。
var result = context.ProcessStatusLog.Join(context.Booking, s => s.PNNumber, b => b.PNNumber, (s, b) => new { booking = b, StatusLog = s).Where(BookingAndStatusLog => BookingAndStatusLog.StatusLog.Status == your_parameter)
.OrderByDescending(BookingAndStatusLog => BookingAndStatusLog.StatusLog.UpdatedOn)
.Take(1);
試試下面的查詢。
context.ProcessStatusLog.where(x=>x.Status == "FOR REVIEW")
.OrderByDescending(x => x.UpdatedOn)
.ToList();
到目前為止我對你的問題的理解,你可以嘗試 -
var result= context.ProcessStatusLog.OrderByDescending(x => x.UpdatedOn)
.FirstOrDefault(x=>x.Status == "CORRECTION");
要么
var result= context.ProcessStatusLog.where(x=>x.Status == "CORRECTION")
.OrderByDescending(x => x.UpdatedOn)
.FirstOrDefault();
它返回您的ProcessStatusLog
class 的單個 object。
using (var ctx = new SchoolDBEntities())
{
var student = ctx.Students
.SqlQuery("Select top1 from ProcessStatusLog where Status like '%"+@APPROVED+"%' order by UpdatedOn", new SqlParameter("@APPROVE", yourinputparameter))
.FirstOrDefault();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.