简体   繁体   中英

Sub Select into LINQ

I am rather new to LINQ and I wrote a query in SQL that I am not quite sure how to convert to a LINQ statement. What I have causes a syntax error on the MAX(), probably because MAX() cannot operate on a datetime value

Any help is appreciated.

SQL

select COUNT(distinct Club) as total
FROM GCClubData 
where NCBClub = 0 
and Date = (select max(Date) from GCClubData)

LINQ

var db = new EFContext();
var data = db.GCClubDatas.Where(x => x.Date.Equals(db.GCClubDatas.Max().Date) && !x.NCBClub);
db = new EFContext();
var maxDate = db.GCClubDatas.Max(x => x.Date);
var data = db.GCClubDatas.Where(x => x.Date == maxDate && !x.NCBClub);

就数据库命中而言,这将更加有效:

db.GCClubDatas.Where(x => x.Date == db.GCClubDatas.Max(y => y.Date) && x.NCBClub == 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM