开发者

How to translate SQL statement with multiple join conditions based on subquery to LINQ

This might be one of those situations where plain SQL commands are better than LINQ. Here's a simplified version of the SQL statement I'm trying to translate:

SELECT * FROM IDTable AS idt 
INNER JOIN NameTable AS nt ON nt.IDTable开发者_Go百科ID=idt.Id 
         AND nt.Id= (SELECT TOP(1) Id 
                     FROM NameTable AS nt2 
                     WHERE nt2.IDTableID=11 ORDER BY nt2.DateInserted DESC) 

I have the LINQ query to pull records when just joining on IDs and I've seen how to join on multiple columns, but I have no idea how to plug the subquery into the mix. If this isnt entirely clear, please let me know and I'll edit to elaborate.


Maybe something like this?

var results = from id in db.IDTable
              join n in db.NameTable on id.Id equals n.IDTableID
              where n.Id = (
                  from n2 in db.NameTable
                  where n2.IDTableID = 11
                  orderby n2.DateInserted desc
                  ).First()
              select new { id, n };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜