2016-03-02 55 views
1
  var flowers = from f in c.Product 
           select new 
           { 
            f.ProductID, 
            f.Price, 
            f.Name, 
            f.Description 
           }; 
       List<int> IDsToRemove = new List<int>(); 

       foreach(var newRow in flowers) 
       { 
        if (((List<int>)Session["UsedIDs"]).Contains(newRow.ProductID)) 
        { 
         IDsToRemove.Add(newRow.ProductID); 
        } 
       } 

我已經得到了ID的,我想刪除那裏是usedIDs並從查詢中productIDs之間的匹配刪除項目,所以我把所有的匹配到整數IDToRemove的列表。現在我不知道該怎麼做:如何從IQueryable的匿名類型

 foreach(var id in IDsToRemove){ 
     flowers.remove(id) } 

    ddlFlowers.DataSource = flowers.ToList(); 
+0

刪除項目的相反,你可以do'ddlFlowers.DataSource = flowers.Except(IDS ();' – shahkalpesh

+0

@caleb我編輯了我的答案 –

+0

或者,'flowers.RemoveAll(f => ids.Contains(f));'將移除這些項目。 – shahkalpesh

回答

2

你可以做這樣的事情:

var listInSession = (List<int>)Session["UsedIDs"]); 
ddlFlowers.DataSource = flowers.Where(f => !listInSession.Contains(f.ProductID)).ToList();