0

我正在尝试将此 SQL 语句转换为 LINQ

SELECT     
  a.UserId, b.Date, a.Value1
FROM         
  web.Table2 AS b
LEFT OUTER JOIN
  web.Table1 AS a 
ON 
  b.Date = a.CreateDate AND UserId = 1

这就是我所拥有的不起作用:

Dim query = From a In ctx.Table1 _
        Group Join b In ctx.Table2 On b.Date Equals a.CreateDate Into group1 = Group
        From x In group1.DefaultIfEmpty() _
        Where ls.UserId = 1 _
        Select a.UserId, x.Date, a.Value1
4

2 回答 2

1

您的代码应该是(检查别名 b,您使用的是 x)

Dim query = From a In ctx.Table1 _
        Join b In ctx.Table2 _ 
        On b.Date Equals a.CreateDate Into group1 = Group _
        Where a.UserId = 1 _
        From b In group1.DefaultIfEmpty() _            
        Select a.UserId, b.Date, a.Value1
于 2012-09-28T17:43:15.333 回答
0

您始终可以直接在 LINQ To SQL 中执行查询,例如:

IEnumerable<MyClass> results = db.ExecuteQuery<MyClass>(@"
    SELECT a.UserId, b.Date, a.Value1 
    FROM web.Table2 AS b 
    LEFT OUTER JOIN web.Table1 AS a ON b.Date = a.CreateDate 
        AND UserId = 1");
于 2012-09-28T17:59:12.390 回答