Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带子查询的Linq查询_C#_Linq - Fatal编程技术网

C# 带子查询的Linq查询

C# 带子查询的Linq查询,c#,linq,C#,Linq,我有一个名为UserPaymentHistory的实体,Id为,UserId为,Price为,PaymentDate为。 我需要得到那一行,其中将有一个用户的最后付款日期 我可以这样做: var lastPaymentDate = db.Repository<UserPayment>() .Where(u => u.UserId == 1) .Select(x => x.PaymentDate)

我有一个名为UserPaymentHistory的实体,Id为,UserId为,Price为,PaymentDate为。 我需要得到那一行,其中将有一个用户的最后付款日期

我可以这样做:

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

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

我有没有办法在一次查询中获得该记录?:)

按付款日期降序排列付款,然后选择第一个:

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