2017-09-13 53 views
-1

我有與需要作爲一個「和」操作的匹配濾波器的一組陣列。餘的過濾器數組爲空應當interpreeted作爲包括用於該過濾器的所有內容:LINQ條件和

List<string> cars = new List<string>(); 
List<string> flights = new List<string>(); 
      flights.Add("Boing"); 
      flights.Add("Airbus"); 

      var transports = new List<Transport>(); 
      var t1 = new Transport("Ford", "Boing"); 
      var t2 = new Transport("BMW", "Boing"); 
      var t3 = new Transport("BMW", "Flights INC"); 

      transports.Add(t1); 
      transports.Add(t2); 
      transports.Add(t3); 

      //returns 0 result. In this scenario I would expect to get all with boing and Airbus and cars excluded from search because its empty 
      var result = transports.Where(p => 
       (cars.Count>0 && cars.Contains(p.CarType)) && 
       (flights.Count > 0 && flights.Contains(p.FlightType))).ToList(); 


     cars = new List<string>(); 
      flights = new List<string>(); 
      cars.Add("BMW"); 
      flights.Add("Boing"); 

      IEnumerable<Transport> result2 = transports; 

      if (cars?.Any() == true) 
      { 
       result2 = transports.Where(p => cars.Contains(p.CarType)); 
      } 
      if (flights?.Any() == true) 
      { 
       result2 = transports.Where(p => flights.Contains(p.FlightType)); 
      } 

      // all rows returned here I only want t2 
      var r = result2.ToList(); 

的Rahul引導是在正確的方向。數組不能爲null。解決它:

     statusViewResult = statusViewResult.Where(p=> 
      (query.ClustersSelected.Count == 0 || query.ClustersSelected.Contains(p.Cluster)) && 
      (query.ParkNamesSelected.Count == 0 || query.ParkNamesSelected.Contains(p.ParkName))); 
+0

這和條件意味着你有第{屬性名}有一定的價值? right – Ajay2707

+0

如果數組不包含任何值,則Contains將爲false。這不是你想要的嗎? –

+0

Somethink like this but but working working statusViewResult = statusViewResult.Where(p => (query.ClustersSelected!= null && query.ClustersSelected.Contains(p.Cluster))&& (query.ParkNamesSelected!= null && query.ParkNamesSelected。含有(p.ParkName))); –

回答

1

那麼你可以使用複合條件像下面考慮ClustersSelected是數組類型

.Where(p => query.ClustersSelected.Length > 0 && query.ClustersSelected.Contains(p.Cluster) 
+0

我試過,但得到:在這種情況下,只支持實體類型,枚舉類型或基元類型 –

+0

statusViewResult = statusViewResult.Where(p => (query.ClustersSelected!= null && query.ClustersSelected.Contains(p.Cluster) )&& (query.ParkNamesSelected = NULL && query.ParkNamesSelected.Contains(p.ParkName!))); –

+0

@ThomasSegato,具有陣列空(未例示)和陣列沒有元素是不同的 – Rahul

2

一個簡單的if會做到這一點:

if(query.ClustersSelected?.Any() == true) 
{ 
    statusViewResult = statusViewResult.Where(p => query.ClustersSelected.Contains(p.Cluster)); 
} 
if(query.ParkNamesSelected?.Any() == true) 
{ 
    statusViewResult = statusViewResult.Where(p => query.ParkNamesSelected.Contains(p.ParkName)); 
} 

每個考慮又增加了過濾器收集。我經常在Entity Framework(和IQueryable)中使用這樣的代碼來生成更小的SQL查詢,但它對於LINQ to object(IEnumerable)也同樣適用。
我使用ClustersSelected?.Any() == true搭上null屬性這兩種情況下,和一個空的集合。

+0

但是這裏的問題是,如果兩個數組都有值,那麼第二個語句只會是第一個語句的子集?下面dosent工作的代碼,但它是什麼,我試圖做的:statusViewResult = statusViewResult.Where(P => (query.ClustersSelected = NULL && query.ClustersSelected.Contains(p.Cluster))&& (查詢! ParkNamesSelected!= null && query.ParkNamesSelected.Contains(p.ParkName))); –

+0

@ThomasSegato - 如果兩個數組都有值,則應滿足兩個條件。我認爲你的代碼行爲相同。 – Kobi

+0

爲什麼不用數組的'Length'屬性來代替使用'Any()'? – Rahul