C# 如何在左外部联接中添加数组元素检查(在SQL操作中)?

C# 如何在左外部联接中添加数组元素检查(在SQL操作中)?,c#,.net,sql-server,entity-framework,linq,C#,.net,Sql Server,Entity Framework,Linq,我有一个SQL查询 SELECT [c].[ClinicID], [c0].[ClinicNotificationId], [c0].[AccountRoleMemberId], [c0].[ClinicId], [c0].[DoctorId], [c0].[Email], [c0].[NotificationType], [c0].[SMS] FROM [Clinic] AS [c] LEFT JOIN [ClinicNotificationRecipient] AS [c0] ON [c].

我有一个SQL查询

SELECT [c].[ClinicID], [c0].[ClinicNotificationId], [c0].[AccountRoleMemberId], [c0].[ClinicId], [c0].[DoctorId], [c0].[Email], [c0].[NotificationType], [c0].[SMS]
FROM [Clinic] AS [c]
LEFT JOIN [ClinicNotificationRecipient] AS [c0] ON [c].[ClinicID] = [c0].[ClinicId] and 
[c0].[AccountRoleMemberId] IN (N'2663c1dc-b414-43a8-9a89-8ff76ebd87e1', N'34273572-b322-45c1-9c95-ce00d29e843d', N'3be573bc-499e-40ba-b245-ce7950123d25', N'6ad82ec2-f7a5-4550-84f2-c3531a2909bf', N'6dd9ac49-8c46-491a-84db-6807efd04935', N'70e47962-4509-4f70-b958-e0eaeced07a8', N'70f651fc-9df8-4342-9678-433b0e63717e', N'b469cd6e-ceb0-46f8-b70e-045c6afcb207', N'b7aa5a0b-f00d-4c28-9ba5-745dcf77135d', N'f6425f8e-f124-4e64-ba6b-ea11bfd81078', N'ff4cedad-80b3-498c-b1ff-e893a18aea09')
WHERE (([c].[Active] = CAST(1 AS bit)) AND ([c].[DoctorID] = @__doctorid_0))
我想在LINQ C#中执行此查询。首先,我尝试了这个LINQ查询:

var q = from c in Clinic
            join cnr in ClinicNotificationRecipient on c.ClinicID equals cnr.ClinicId into g
            from gcnr in g.DefaultIfEmpty()
            where c.Active == true && c.DoctorID == doctorid && (gcnr == null || subUserAcctRoleMemberIds.Contains(gcnr.AccountRoleMemberId))
            select new {
                c.ClinicID,
                cnr = gcnr
            };
然而,这给了我这个sql查询

SELECT [c].[ClinicID], [c0].[ClinicNotificationId], [c0].[AccountRoleMemberId], [c0].[ClinicId], [c0].[DoctorId], [c0].[Email], [c0].[NotificationType], [c0].[SMS]
FROM [Clinic] AS [c]
LEFT JOIN [ClinicNotificationRecipient] AS [c0] ON [c].[ClinicID] = [c0].[ClinicId]
WHERE (([c].[Active] = CAST(1 AS bit)) AND ([c].[DoctorID] = @__doctorid_0)) AND (([c0].[ClinicNotificationId] IS NULL OR [c0].[AccountRoleMemberId] IS NULL) OR [c0].[AccountRoleMemberId] IN (N'2663c1dc-b414-43a8-9a89-8ff76ebd87e1', N'34273572-b322-45c1-9c95-ce00d29e843d', N'3be573bc-499e-40ba-b245-ce7950123d25', N'6ad82ec2-f7a5-4550-84f2-c3531a2909bf', N'6dd9ac49-8c46-491a-84db-6807efd04935', N'70e47962-4509-4f70-b958-e0eaeced07a8', N'70f651fc-9df8-4342-9678-433b0e63717e', N'b469cd6e-ceb0-46f8-b70e-045c6afcb207', N'b7aa5a0b-f00d-4c28-9ba5-745dcf77135d', N'f6425f8e-f124-4e64-ba6b-ea11bfd81078', N'ff4cedad-80b3-498c-b1ff-e893a18aea09'))

作为左联接操作的一部分,如何在条件中移动

我想我已经用这个查询解决了这个问题

var q = from c in Clinic
            join cnr in ClinicNotificationRecipient.Where(x => subUserAcctRoleMemberIds.Contains(x.AccountRoleMemberId))
            on c.ClinicID equals cnr.ClinicId into g
            from gcnr in g.DefaultIfEmpty()
            let ids = subUserAcctRoleMemberIds
            where c.Active == true && c.DoctorID == doctorid
            select new {
                c.ClinicID,
                cnr = gcnr
            };
这将生成以下sql查询:

SELECT [c].[ClinicID], [t].[ClinicNotificationId], [t].[AccountRoleMemberId], [t].[ClinicId], [t].[DoctorId], [t].[Email], [t].[NotificationType], [t].[SMS]
FROM [Clinic] AS [c]
LEFT JOIN (
    SELECT [c0].[ClinicNotificationId], [c0].[AccountRoleMemberId], [c0].[ClinicId], [c0].[DoctorId], [c0].[Email], [c0].[NotificationType], [c0].[SMS]
    FROM [ClinicNotificationRecipient] AS [c0]
    WHERE [c0].[AccountRoleMemberId] IN (N'2663c1dc-b414-43a8-9a89-8ff76ebd87e1', N'34273572-b322-45c1-9c95-ce00d29e843d', N'3be573bc-499e-40ba-b245-ce7950123d25', N'6ad82ec2-f7a5-4550-84f2-c3531a2909bf', N'6dd9ac49-8c46-491a-84db-6807efd04935', N'70e47962-4509-4f70-b958-e0eaeced07a8', N'70f651fc-9df8-4342-9678-433b0e63717e', N'b469cd6e-ceb0-46f8-b70e-045c6afcb207', N'b7aa5a0b-f00d-4c28-9ba5-745dcf77135d', N'f6425f8e-f124-4e64-ba6b-ea11bfd81078', N'ff4cedad-80b3-498c-b1ff-e893a18aea09')
) AS [t] ON [c].[ClinicID] = [t].[ClinicId]
WHERE ([c].[Active] = CAST(1 AS bit)) AND ([c].[DoctorID] = @__doctorid_0)

在原始查询中,
ClinicNotificationRecipient
针对
LEFT JOIN的
ON
子句中的数组进行筛选

在LINQ中匹配此项的正确方法是将其置于
DefaultIfEmpty()之前

var q=来自临床中的c
将cnr加入ClinicNotificationRecipient on c.ClinicID等于cnr.ClinicID进入g
从g.Where中的gcnr(gcnr2=>subuseractrolememberids.Contains(gcnr2.AccountRoleMemberId)).DefaultIfEmpty()
其中c.Active==true&&c.DoctorID==DoctorID
选择新的{
c、 临床医生,
cnr=gcnr
};

我收到此错误InvalidOperationException:无法转换LINQ表达式'DbSet().GroupJoin(内部:DbSet(),outerKeySelector:c=>(可为null)c.ClinicID,innerKeySelector:cnr=>cnr.ClinicID,resultSelector:(c,g)=>new{c=c,g=g}'。以可以翻译的形式重写查询,或者通过插入对“AsEnumerable”、“asAsAsAsyncEnumerable”、“ToList”或“ToListSync”的调用显式切换到客户端计算。