我不擅长
Linq表达,今天我遇到了一个奇怪的问题,如下面的内连接语句,
var orders = (from q in dao.CurrentDBContext.New_OrderForm join d in dao.CurrentDBContext.New_OrderGoodsDetail on q.billNum equals d.billNum select new { q.billNum,q.orderSource,q.sourceOddNum d.PPT }
当我追踪linq语句时,我很困惑,实体框架会将linq语句转换为下面的sql语句
SELECT [Extent1].[billNum] AS [billNum],[Extent1].[orderSource] AS [orderSource],[Extent1].[sourceOddNum] AS [sourceOddNum],[Extent2].[PPT] AS [PPT] FROM [dbo].[New_OrderForm] AS [Extent1] INNER JOIN [dbo].[New_OrderGoodsDetail] AS [Extent2] ON ([Extent1].[billNum] = [Extent2].[billNum]) OR (([Extent1].[billNum] IS NULL) AND ([Extent2].[billNum] IS NULL))
或(([Extent1].[billNum] IS NULL)AND([Extent2].[billNum] IS NULL)“
解决方法
如果您无法将billNum列更改为不可为空,则可以执行以下操作.
首先,设置@Giorgi提到的选项
class CurrentDBContext { public CurrentDBContext() { Configuration.UseDatabaseNullSemantics = true; // ... } }
然后将LINQ查询更改为不使用join,但这样简单
var orders = (from q in dao.CurrentDBContext.New_OrderForm from d in dao.CurrentDBContext.New_OrderGoodsDetail where q.billNum == d.billNum select ...