1. 程式人生 > >Linq實現sql一張表資料不存在另外一張表

Linq實現sql一張表資料不存在另外一張表

Linq中包含查詢

//Linq程式碼:
T_WxMaterials.Where(n=>T_VideoMsgs.Select(m=>m.MediaID).Contains(n.MediaID)) 

//生成的sql語句:
SELECT [t0].[ID], [t0].[Type], [t0].[Title], [t0].[Cnt], [t0].[MediaID], [t0].[LocalPath], [t0].[WxPath]
FROM [T_WxMaterial] AS [t0]
WHERE EXISTS(
    SELECT NULL AS [EMPTY]
    FROM [T_VideoMsg] AS
[t1] WHERE [t1].[MediaID] = [t0].[MediaID] )

Linq中不包含查詢

//Linq程式碼:
T_WxMaterials.Where(n=>!T_VideoMsgs.Select(m=>m.MediaID).Contains(n.MediaID)) 

//生成的sql語句:
SELECT [t0].[ID], [t0].[Type], [t0].[Title], [t0].[Cnt], [t0].[MediaID], [t0].[LocalPath], [t0].[WxPath]
FROM [T_WxMaterial] AS [t0]
WHERE
NOT (EXISTS( SELECT NULL AS [EMPTY] FROM [T_VideoMsg] AS [t1] WHERE [t1].[MediaID] = [t0].[MediaID] ))