C# LINQ检查值不在自定义对象的多个列表中
我无法将以下SQL查询转换为C#中的lambda表达式。我试过join,但它给了我错误 SQL查询是:C# LINQ检查值不在自定义对象的多个列表中,c#,lambda,C#,Lambda,我无法将以下SQL查询转换为C#中的lambda表达式。我试过join,但它给了我错误 SQL查询是: SELECT DISTINCT Customer.* FROM Customer INNER JOIN RouteCustomer ON Customer.CustomerId = RouteCustomer.CustomerId WHERE RouteCustomer.RouteId = @RouteId AND
SELECT DISTINCT Customer.* FROM Customer INNER JOIN RouteCustomer ON Customer.CustomerId = RouteCustomer.CustomerId
WHERE
RouteCustomer.RouteId = @RouteId AND
Customer.Inactive = 0 AND
AND
(
(Customer.CustomerId NOT IN (SELECT CustomerId FROM RouteStopScheduleRule WHERE @Date >= EffectiveDate AND Inactive = 0))
OR
(
(Customer.CustomerId IN (SELECT CustomerId FROM RouteStopScheduleRule WHERE @Date >= EffectiveDate AND Inactive = 0 AND WeeklyFrequency = 0))
AND
(Customer.CustomerId NOT IN (SELECT CustomerId FROM RouteStopScheduleRule WHERE @Date >= EffectiveDate AND Inactive = 0 AND WeeklyFrequency != 0))
)
)
我在C#中尝试过的代码是
IEnumerable RSRList1=App.Database.AllRouteStopScheduleRule.Where(rsr1=>rsr1.EffectiveDate>=Date&&rsr1.Inactive==false)。AsEnumerable();
IEnumerable RSRList2=App.Database.AllRouteStopScheduleRule.Where(rsr2=>rsr2.EffectiveDate>=日期和rsr2.Inactive==假和rsr2.WeeklyFrequency==0)。AsEnumerable();
IEnumerable RSRList3=App.Database.AllRouteStopScheduleRule.Where(rsr3=>rsr3.EffectiveDate>=日期和&rsr3.Inactive==false和&rsr3.WeeklyFrequency!=0)。AsEnumerable();
列表_结果=新列表();
var\u Prelist=App.Database.AllCustomer.Where(c1=>c1.Inactive==false)
.Join(App.Database.AllRouteCustomer.Where(rc1=>rc1.RouteId==RouteId&&(rc1.EffectiveDate new{CustomerId=c.CustomerId})
其中(x=>(!RSRList1.Contains(x.CustomerId))| |(RSRList2.Contains(x.CustomerId)&(!RSRList3.Contains(x.CustomerId)));
但它在所有列表RSRList1、RSRList2和RSRList3中都给了我错误,因为IEnumerable不包含“contains”的定义
我缺少什么?要在Linq To Sql中使用Contains,列表需要是简单的类型,而不是复杂的对象(以便提供程序在子句中将其转换为
)。因此,您需要将列表更改为:
var RSRList1 = App.Database.AllRouteStopScheduleRule.Where(rsr1 =>
rsr1.EffectiveDate >= Date && rsr1.Inactive == false)
.AsEnumerable().Select(x=> x.CustomerId);
其他两个列表也是如此
当您需要对特定属性使用Contains时,比如说and integer,那么列表
就是您需要的序列。您可以使用选择
(又称投影)来完成此操作
var RSRList1 = App.Database.AllRouteStopScheduleRule.Where(rsr1 =>
rsr1.EffectiveDate >= Date && rsr1.Inactive == false)
.AsEnumerable().Select(x=> x.CustomerId);