2016-08-15 22 views
3

我无法将以下SQL查询转换为C#中的lambda表达式。我尝试加入但它给我错误。LINQ检查值不在自定义对象的多个列表中

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 <>多年平均值包含定义'Contains'

我缺少什么?

回答

1

要使用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(又称投影)来做到这一点。