求一SQL语句,在EF中写法
select n.* ,b.FileName from NewsInfo n left join
(select fid,filename from brandfile where typeid=2 and fileid in (select max(fileid) from brandfile group by fid) ) b
on n.newsid=b.fid
[解决办法]
var maxids = brandfile.GroupBy(x => x.fid).Select(g => g.Max(x => x.fieldid));
var b = brandfile.Where(x => x.typeid == 2 && maxids.Contains(x => x.fileid));
var result = from n in NewsInfo
join b in brandfile on n.newsid equals b.fid into n1
from n2 in n1.DefaultIfEmpty()
select new
{
NewsInfo = n,
FileName = b.filename
};
[解决办法]
var query=from n in db.NewsInfo
let m=db.brandfile.GroupBy(y=>y.fid).Select(g=>g.Max(z=>z.fieldid)
join b in db.brandfile.Where(x=> x.typeid==2 &&
m.Contains(x=>x.fieldid))
on n.newsid equals b.fid into Left
from b in Left.DefalutIfEmpty()
select new {n,FileName=b==null?"":b.FileName};