日期:2014-05-20  浏览次数:20774 次

求一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

------解决方案--------------------
C# code
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
             };

------解决方案--------------------
探讨
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