gpt4 book ai didi

c# - LINQ 检查值不在自定义对象的多个列表中

转载 作者:行者123 更新时间:2023-11-30 17:36:48 25 4
gpt4 key购买 nike

我无法将以下 SQL 查询转换为 C# 中的 lambda 表达式。我试过加入,但它给了我错误。

SQL 查询是:

            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<RouteStopScheduleRule> RSRList1 = App.Database.AllRouteStopScheduleRule.Where(rsr1 => rsr1.EffectiveDate >= Date && rsr1.Inactive == false).AsEnumerable();
IEnumerable<RouteStopScheduleRule> RSRList2 = App.Database.AllRouteStopScheduleRule.Where(rsr2 => rsr2.EffectiveDate >= Date && rsr2.Inactive == false && rsr2.WeeklyFrequency == 0).AsEnumerable();
IEnumerable<RouteStopScheduleRule> RSRList3 = App.Database.AllRouteStopScheduleRule.Where(rsr3 => rsr3.EffectiveDate >= Date && rsr3.Inactive == false && rsr3.WeeklyFrequency != 0).AsEnumerable();

List<Customer> _Result = new List<Customer>();
var _Prelist = App.Database.AllCustomer.Where(c1 => c1.Inactive == false)
.Join(App.Database.AllRouteCustomer.Where(rc1 => rc1.RouteId == RouteId && (rc1.EffectiveDate <= Date && (rc1.ExpiryDate <= Date || rc1.ExpiryDate.Value.AddYears(1) <= Date))),
rc => rc.CustomerId,
c => c.CustomerId,
(rc, c) => new { CustomerId = c.CustomerId })
.Where(x => (!RSRList1.Contains(x.CustomerId)) || (RSRList2.Contains(x.CustomerId) && (!RSRList3.Contains(x.CustomerId))));

但它在所有列表 RSRList1、RSRList2 和 RSRList3 中都给我错误,因为 IEnumerable<> 不包含“包含”的定义

我缺少什么?

最佳答案

要在 Linq to Sql 中使用 Contains,列表需要是简单类型而不是复杂对象(以便提供者将其简单地转换为 IN 子句)。因此,您需要将列表更改为:

var RSRList1 = App.Database.AllRouteStopScheduleRule.Where(rsr1 => 
rsr1.EffectiveDate >= Date && rsr1.Inactive == false)
.AsEnumerable().Select(x=> x.CustomerId);

其他两个列表也是如此。

当您需要为特定属性使用 Contains 时,假设是整数,然后是 List<int>是你需要的顺序。您可以使用 Select 来做到这一点(又名投影)。

关于c# - LINQ 检查值不在自定义对象的多个列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38959962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com