C# 实体框架6-外部联接和方法语法查询
我正在尝试使用Entity Framework 6重新编写以下SQL左外部联接查询:C# 实体框架6-外部联接和方法语法查询,c#,linq,entity-framework-6,linq-method-syntax,C#,Linq,Entity Framework 6,Linq Method Syntax,我正在尝试使用Entity Framework 6重新编写以下SQL左外部联接查询: select tblA.*, tblB.* from dbo.TableA tblA left outer join dbo.TableB tblB on tblA.Student_id=tblB.StudentId and tblA.other_id=tblB.OtherId where tblB.Id is null 这是我目前的C代码: 下面是我得到的编译器错误: 无法从用法推断类型参数。尝试
select tblA.*, tblB.*
from dbo.TableA tblA left outer join dbo.TableB tblB on
tblA.Student_id=tblB.StudentId and tblA.other_id=tblB.OtherId
where tblB.Id is null
这是我目前的C代码:
下面是我得到的编译器错误:
无法从用法推断类型参数。尝试指定
类型参数被显式删除
简而言之:我需要外部连接通过实体框架提供的两个DbSet对象,使用连接中的多个列
我也相当肯定这不会正确地执行左外部联接,即使我没有收到编译器错误;我怀疑我需要在某个地方涉及DefaultIfEmpty()
方法。如果你也能帮我的话,我会给你加分的
更新#1:如果我在连接中使用强类型,它会起作用。。。它只是无法处理匿名类型,还是我做错了什么
public class StudentOther
{
public int StudentId { get; set; }
public int OtherId { get; set; }
}
using (var db = EFClass.CreateNewInstance())
{
var results = db.TableA.GroupJoin(
db.TableB,
tblA => new StudentOther { StudentId = tblA.Student_id, OtherId = tblA.other_id },
tblB => new StudentOther { StudentId = tblB.StudentId, OtherId = tblB.OtherId },
(tblA, tblB) => new { TableAData = tblA, TableBData = tblB }
)
.Where(x => x.TableBData.Id == null)
.AsNoTracking()
.ToList();
return results;
}
你能试试这个解决方案吗?我不确定结果:(
他正在询问方法语法。
public class StudentOther
{
public int StudentId { get; set; }
public int OtherId { get; set; }
}
using (var db = EFClass.CreateNewInstance())
{
var results = db.TableA.GroupJoin(
db.TableB,
tblA => new StudentOther { StudentId = tblA.Student_id, OtherId = tblA.other_id },
tblB => new StudentOther { StudentId = tblB.StudentId, OtherId = tblB.OtherId },
(tblA, tblB) => new { TableAData = tblA, TableBData = tblB }
)
.Where(x => x.TableBData.Id == null)
.AsNoTracking()
.ToList();
return results;
}
(from tblA in dbo.TableA
join tblB in dbo.TableB on new { tblA.Student_id, tblA.other_id } equals new { blB.StudentId, tblB.OtherId }
into tblBJoined
from tblBResult in tblBJoined.DefaultIfEmpty()
where tblBResult.Id == null
select new {
TableAData = tblA,
TableBData = tblB
}).ToList();