2013-07-26 39 views
-1

如何在查詢中動態添加OR條件。代碼使用EF5.0,LINQ,MVC4.0和存儲庫模式。當數值爲動態時查詢OR條件

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);  
} 
+0

使用'PredicateBuilder'。 –

回答

1

我不知道你需要PredicateBuilder在這種情況下

Contains應該夠了。

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));