如何从 IQueryable 匿名类型中删除项目

本文关键字:类型 删除项目 IQueryable | 更新日期: 2023-09-27 18:37:07

           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,其中使用了 ID 和查询中的产品 ID 之间存在匹配项,因此我已将所有匹配的整数放入 IDsToRemove 列表中。现在我不知道该怎么做:

     foreach(var id in IDsToRemove){
         flowers.remove(id) }
     ddlFlowers.DataSource = flowers.ToList();

如何从 IQueryable 匿名类型中删除项目

你可以做这样的事情:

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