如何从列表中删除 ID 不在 ID 列表中的项目
How to remove item from list which Id is not in list of Id's
我有一个 table 看起来像这样:
ID ProductId OrderId
-- --------- --------
1 1 1
2 2 1
3 4 1
4 2 2
5 6 2
6 7 2
首先我得到所有具有相同 OrderId 的 ProdictId
var listOfProdictids = db.OrderedProducts.Where(x => x.OrderId == 1).Select(x => x.ProductId).toArray();
我希望它看起来像 1、2、4
我还有一个table产品
ID Brand Mark
-- --------- --------
1 Samsung Galaxy s10
2 Apple Iphone 7
3 Xiaomi Mi9
4 Huawei Honor
5 Sony Xperia
从数据库中获取 ID 为 1、2、4 的所有产品的最佳方法是什么?
我的看法:
List<ProductVM> products = db.Products.Select(x => x).toList();
列出所有产品并以某种方式删除不在数组 "listOfProductids" 中的 ID 3 和 5。
或
List<ProductVM> products = db.Products.Where(x => x.id == listOfProductids).Select(x => x).toList();
不确定 lambda Where(x => x.id != listOfProdictids)
是否有效。
这个想法是将每个 ProductId 与数组中的 Id 进行比较。如果匹配比 select
知道怎么做吗?
最终列表应如下所示:
ID Brand Mark
-- --------- --------
1 Samsung Galaxy s10
2 Apple Iphone 7
4 Huawei Honor
更新:基于回答 npinti
如果我想做 2 个订单,应该像这样吗?
var orders = db.Orders.Select(x => x.ProductId).toArray();
to get Array with two orders 1 and 2
比
List<ProductVM> products = new List<ProductVM>();
foreach (ord o in orders)
{
// getting all product id's for first OrderId
HashSet<int> listOfProdictids = new HashSet<int>(db.OrderedProducts.Where(x => x.OrderId == o).Select(x => x.ProductId).toArray());
//getting list of products
var qwe = db.Products.Where(x => !listOfProducts.Contains(x.id)).Select(x => x).toList();
//Add list
products.Add(qwe)
}
return View (products);
获取订单的所有产品 ID
var listOfProdictids = db.OrderedProducts.Where(x => x.OrderId == 1).Select(x => x.ProductId).ToArray();
您需要减去所有订购的产品。
List<ProductVM> removedProducts = db.Products.Where(c=>!listOfProdictids.Contains(c=>c.Id)).ToList();
并从产品 table 中删除列表。
您可以尝试这样做:
HashSet<int> listOfProdictids = new HashSet<int>(db.OrderedProducts.Where(x => x.Id == 1).Select(x => x.ProductId).toArray());
然后:
List<ProductVM> products = db.Products.Where(x => !listOfProducts.Contains(x.id)).Select(x => x).toList();
我有一个 table 看起来像这样:
ID ProductId OrderId
-- --------- --------
1 1 1
2 2 1
3 4 1
4 2 2
5 6 2
6 7 2
首先我得到所有具有相同 OrderId 的 ProdictId
var listOfProdictids = db.OrderedProducts.Where(x => x.OrderId == 1).Select(x => x.ProductId).toArray();
我希望它看起来像 1、2、4
我还有一个table产品
ID Brand Mark
-- --------- --------
1 Samsung Galaxy s10
2 Apple Iphone 7
3 Xiaomi Mi9
4 Huawei Honor
5 Sony Xperia
从数据库中获取 ID 为 1、2、4 的所有产品的最佳方法是什么? 我的看法:
List<ProductVM> products = db.Products.Select(x => x).toList();
列出所有产品并以某种方式删除不在数组 "listOfProductids" 中的 ID 3 和 5。
或
List<ProductVM> products = db.Products.Where(x => x.id == listOfProductids).Select(x => x).toList();
不确定 lambda Where(x => x.id != listOfProdictids)
是否有效。
这个想法是将每个 ProductId 与数组中的 Id 进行比较。如果匹配比 select
知道怎么做吗?
最终列表应如下所示:
ID Brand Mark
-- --------- --------
1 Samsung Galaxy s10
2 Apple Iphone 7
4 Huawei Honor
更新:基于回答 npinti
如果我想做 2 个订单,应该像这样吗?
var orders = db.Orders.Select(x => x.ProductId).toArray();
to get Array with two orders 1 and 2
比
List<ProductVM> products = new List<ProductVM>();
foreach (ord o in orders)
{
// getting all product id's for first OrderId
HashSet<int> listOfProdictids = new HashSet<int>(db.OrderedProducts.Where(x => x.OrderId == o).Select(x => x.ProductId).toArray());
//getting list of products
var qwe = db.Products.Where(x => !listOfProducts.Contains(x.id)).Select(x => x).toList();
//Add list
products.Add(qwe)
}
return View (products);
获取订单的所有产品 ID
var listOfProdictids = db.OrderedProducts.Where(x => x.OrderId == 1).Select(x => x.ProductId).ToArray();
您需要减去所有订购的产品。
List<ProductVM> removedProducts = db.Products.Where(c=>!listOfProdictids.Contains(c=>c.Id)).ToList();
并从产品 table 中删除列表。
您可以尝试这样做:
HashSet<int> listOfProdictids = new HashSet<int>(db.OrderedProducts.Where(x => x.Id == 1).Select(x => x.ProductId).toArray());
然后:
List<ProductVM> products = db.Products.Where(x => !listOfProducts.Contains(x.id)).Select(x => x).toList();