-1

How to add OR condition dynamically in query. Code Using EF5.0, LINQ , MVC4.0 and repository pattern.

public ActionResult Index()
    {
      var model = new AssignedSearchFields();   //ViewModel
      List<LClient.Model.Attribute> allFields =                      Common.GetAssignedSearchFields(User.Identity.Name, userID, "EntityType Name 1");     
      model.SearchFields = allFields;  // List allFields having dynamic number of items.
      model.Entities = entityRepository.GetMany(e => e.EntityTypeID == 1 && e.InstanceID <= 10 && (e.AttributeID == 1 || e.AttributeID == 2 || e.AttributeID == 3 || e.AttributeID == 4)); // Here I want to use AttributeID dynamically without OR conditions.
      return View(model);    
}  
Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
kpr
  • 451
  • 5
  • 17

1 Answers1

1

I'm not sure you need PredicateBuilder in this case

Contains should be enough.

var dynamicIds = new[]{1, 2, 3, 4};

//or something like that, if you want values coming from 'GetAssignedSearchFields'
var dynamicIds = Common.GetAssignedSearchFields(User.Identity.Name, userID, "EntityType Name 1")
                   .Select(m => m.Id);
//xxx
      model.Entities = entityRepository.GetMany(e => 
                          e.EntityTypeID == 1 && 
                          e.InstanceID <= 10 
                          && (dynamicIds.Contains(e.AttributeID));
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122