簡體   English   中英

Linq查詢子查詢

[英]Linq query with subquery

我有一個名為UserPaymentHistory的實體,包含Id,UserId,Price,PaymentDate。 我需要獲得具有最后PaymentDate的User的那一行。

我可以這樣查詢:

var lastPaymentDate =
    db.Repository<UserPayment>()
            .Where(u => u.UserId == 1)
            .Select(x => x.PaymentDate)
            .Max();

然后:

var userPayment = db.Repository<UserPayment>()
                      .Where(u => u.UserId == request.UserId 
                      && u.PaymentDate == lastPaymentDate).Single();

有什么辦法可以在一個查詢中獲得該記錄嗎? :)

PaymentDate按降序付款並選擇第一個:

var userPayment = db.Repository<UserPayment>()
                      .Where(u => u.UserId == request.UserId)
                      .OrderByDescending(u => u.PaymentDate)
                      .FirstOrDefault();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM