.net 为什么在我放置&;时lambda表达式将左连接更改为内连接&;条件
我喜欢生成左外部联接查询,如下所示,但在添加 &&o.sgd.intStoreGroup==intStoreGroup 在lambda表达式中,它从左连接变为内连接。 下面的lamdba表达式有错误吗?为什么会改变?那我怎么解决呢 型号.net 为什么在我放置&;时lambda表达式将左连接更改为内连接&;条件,.net,linq,tsql,lambda,.net,Linq,Tsql,Lambda,我喜欢生成左外部联接查询,如下所示,但在添加 &&o.sgd.intStoreGroup==intStoreGroup 在lambda表达式中,它从左连接变为内连接。 下面的lamdba表达式有错误吗?为什么会改变?那我怎么解决呢 型号 public partial class Partner_StoreTbl { [Key] public int intStore { get; set; } public int intPartner {
public partial class Partner_StoreTbl
{
[Key]
public int intStore { get; set; }
public int intPartner { get; set; }
public string varStoreRef { get; set; }
public byte tintStatus { get; set; }
}
public partial class Partner_StoreGroupDetailTbl
{
[Key]
public int intStoreGroupDetail { get; set; }
public int intStoreGroup { get; set; }
public int intStore { get; set; }
}
LINQ
List<byte> byteValue = new List<byte> { 1 };
var StoreGroupDetail = Partner_StoreTbl.GroupJoin(Partner_StoreGroupDetailTbl, s => s.intStoreRef, sgd => sgd.intStore, (s, sgd) => new { s, sgd })
.Where(o=>o.s.intPartner==intPartner)
.SelectMany(o => o.sgd.DefaultIfEmpty(), (s,sgd) => new { s.s,sgd})
.Where(o => byteValue.Contains( o.s.tintStatus ) && o.sgd.intStoreGroup==intStoreGroup)
.Select(o => new MODELS.ViewModels.Partner.StoreGroupDetail
{
intStoreRef = o.s.intStoreRef,
intStoreGroup = o.sgd.intStoreGroup==null?0:o.sgd.intStoreGroup,
}).ToList();
但不知怎的,我得到了这个
SELECT
[Extent1].[intStoreRef] AS [intStoreRef],
[Extent2].[intStoreGroup] AS [C1]
FROM [dbo].[Partner_StoreTbl] AS [Extent1]
INNER JOIN [dbo].[Partner_StoreGroupDetailTbl] AS [Extent2] ON [Extent1].[intStoreRef] = [Extent2].[intStore]
WHERE ([Extent1].[intPartner] = @1) AND ([Extent1].[tintStatus] IN (cast(1 as tinyint))) AND ([Extent2].[intStoreGroup] = @1)
为了
.GroupJoin(Partner_StoreGroupDetailTbl, s => s.intStoreRef, sgd => sgd.intStore, (s, sgd) => new { s, sgd })
你也许应该试试
.GroupJoin(Partner_StoreGroupDetailTbl, s => new { intStore = s.intStoreRef, intStoreGroup = intStoreGroup }, sgd => new { intStore = sgd.intStore, sgd.intStoreGroup }, (s, sgd) => new { s, sgd })
然后删除条件&o.sgd.intStoreGroup==intStoreGroup
但您必须使用EF/linqtosql对其进行测试
关于为什么。。。我不知道确切的原因,但我可以告诉你:
SELECT * FROM A LEFT JOIN B ON something WHERE B.Something = C
相当于
SELECT * FROM A INNER JOIN B ON something WHERE B.Something = C
因为
其中B.Something=C
强烈暗示B.Something不是空的
,这强烈暗示B已经被“找到”。因此,如果B未“找到”,则其中的将“失败”,并且无法返回该行。就像一个内部连接
HI xanatos,当我在这行sgd=>{intStore=sgd.intStore,sgd.intStoreGroup}@tsohtan上使用您的行“并非所有路径返回lambda..”时,我遇到了这个错误。我忘记了新的:-)现在重试。然后记得删除&&o.sgd.intStoreGroup==intStoreGroup
再次感谢您的回复,我现在需要的就是它。但是你能解释一下这一行s=>new{intStore=s.intStoreRef,intStoreGroup=intStoreGroup},sgd=>new{intStore=sgd.intStore,sgd.intStoreGroup}的用途吗在第二部分,我不明白为什么你的意思是从左连接B中选择*,其中B.something=C相当于从内部连接B中选择*,其中B.something=C@tsohtan在连接上有两个条件,在它们之间。要在LINQ中执行此操作,必须创建两个包含比较项的对象new{x1=a,x2=b}
,new{x1=c,x2=d}
将转换为a==c&&b==d
SELECT * FROM A INNER JOIN B ON something WHERE B.Something = C