-2

如何将此 SQL 转换为 LINQ?

Select Top 1 iD, Grad from tbl1 
WHERE tbl1.ID not in 
(Select GradId from tbl2 WHERE ID = 99469)
4

2 回答 2

1

看起来最简单的方法是这样的:

var grads = 
    (from r in tbl2 
     where r.ID == 99469 
     select r.GradID);
var results = 
    (from r in tbl1
     where !grads.Contains(r.ID)
     select new { r.ID, r.Grad })
    .FirstOrDefault();

或者更简洁一点:

var grads = ...
var results = tbl1
    .Select(r => new { r.ID, r.Grad })
    .FirstOrDefault(r => !grads.Contains(r.ID));
于 2013-03-26T22:52:53.370 回答
1

类似的东西:

 var query =
         (from c in db.tbl1
          where !(from d in db.tbl2
          select d.id)
               .Contains(c.id)
          select c).Take(1)
于 2013-03-26T23:00:27.890 回答