2012-01-20 20 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

相关问题