【问题标题】:How to remove items from IQueryable anonymous type如何从 IQueryable 匿名类型中删除项目
【发布时间】:2016-03-02 01:09:36
【问题描述】:
           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,其中 usedID 和 productID 之间存在匹配,因此我将所有匹配的整数放入 IDsToRemove 列表中。现在我不知道该怎么做:

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

     ddlFlowers.DataSource = flowers.ToList();

【问题讨论】:

  • 你可以不删除项目,而是ddlFlowers.DataSource = flowers.Except(ids).ToList();
  • @caleb 我已经编辑了我的答案
  • 或者,flowers.RemoveAll(f =&gt; ids.Contains(f)); 将删除原处的项目。

标签: c# asp.net lambda iqueryable


【解决方案1】:

你可以这样做:

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

【讨论】:

    猜你喜欢
    • 2012-08-12
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 2021-05-12
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多