2012-01-20 57 views
0

我有一個小問題,我需要一些幫助。我有一個Restaurant對象,其中包含字段:districtId,dishId,foodCategoryId和restaurantName。在Linq做一些小事

根據我的代碼,我需要從districtId數組輸入參數中檢查它是否在RestaurantTable中匹配。我有一個想法,我應該使用

districtId.ToList().Foreach(blah blah action) 

但我使用它有困難。請指教。提前致謝。

我的代碼片段:

public IEnumerable<Restaurant> GetAllRestaurants(string restaurantName 
     , int[] districtId 
     , int dishId = 0 
     , int foodCategoryId = 0) 
    { 

var q = RestaurantTable.Where(restaurants => restaurants.RestaurantName.Contains(restaurantName.ToLower().Trim()) 
               | restaurants.DishId == dishId 
               | restaurants.FoodCategoryId == foodCategoryId 
| "For each Id's in districtId check if it has a match in restaurants.DistrictId") 

return q.ToList(); 
} 

回答

1

您可以使用Contains()

var q = RestaurantTable.Where(restaurant => restaurant.RestaurantName.Contains(restaurantName.ToLower().Trim()) 
           || restaurant.DishId == dishId 
           || restaurant.FoodCategoryId == foodCategoryId 
           || districtId.Contains(restaurant.DistrictId)) 

而且要使用||(邏輯OR),而不是|(二進制OR)

+0

感謝回答,讓我試試那個。如果可以的話,你能解釋一下那部分嗎?或者我是否正確,如果districtId包含任何restaurant.DistrictId,它將返回true? – grayman

+0

yey!有效!再次感謝:D – grayman

相關問題