Linq To SQL:左连接和vb.NET

时间:2013-12-10 17:27:23

标签: sql vb.net linq

我有这个SQL查询:

SELECT TOP 1 r.* FROM dbo.Request r
  LEFT JOIN dbo.Process p
  ON r.IdReq = p.IdReq
WHERE IdProcess Is NULL
ORDER BY r.ReqDate ASC

我希望使用Linq To SQL语法在vb.net过程中编写它:

Private Function getOldestRequest() As Request
    Dim dc As ProcessDataContext = new ProcessDataContext(ConfigurationManager.ConnectionStrings("...").ConnectionString);

    Dim pr = From r In dc.Request
             From p In dc.Process.Where(Function(v) v.IdReq = r.IdReq And v.idProcess Is Nothing)
             Select New {Request = r}

    Return pr
End Function

这是我写的,但我在Return行上收到错误。

2 个答案:

答案 0 :(得分:1)

LINQ语句返回IEnumerable对象,并且您的方法返回单个Request对象。在你的情况下看起来你只需要返回LINQ语句的第一个结果。

类似的东西:

    Dim pr = (From r In dc.Request
         From p In dc.Process.Where(Function(v) v.IdReq = r.IdReq And v.idProcess Is Nothing)
         Select New {Request = r}).FirstOfDefault
    Return pr

答案 1 :(得分:0)

将您的选择更改为

Select r

你回归

Return pr.OrderBy(Function(v) v.RecDate).FirstOrDefault