[英]Linq query to Check and get dictionary item from list of dictionary items
[英]LINQ query to find items in a list from other list by item
我有代码,它工作正常,但现在想将“糖”更改为 shopResources.Product: List shopResourceProduct> 并通过 id 找到它。 我该怎么做? 之后它应该返回相同的 ProductList
var usedProduct = await this.context.Clients
.Where(r => r.AllowedProduct.Any(u => u.Product.Contains("sugar")))
.Select(r => r)
.ToListAsync(ct);
我查看了其他查询,但我可以找到与 Linq 的密切比较。 您可以指出我的任何想法或任何地方都会有很大帮助。
如果您说您知道如何在 N 个产品列表中查找单个产品,但您现在想在 N 个列表中查找 2+ 个产品,一般模式是:
var iWant = new T[] { some, things, here };
var result = listOfNThings.Where(thing => iWant.Contains(w => w == thing));
假设Client.AllowedProduct
是映射ClientId
和ProductId
的实体的集合(因此应该真正称为AllowedProducts
),您可以询问其 allowedproduct 集合具有任何 x 的客户端,其中x.ProductId
Contain
在列表中你要:
var iWant = new[] { 1, 6, 9, 22 }; //list of 4 productids I want
var usedProduct = await this.context.Clients
.Where(r => r.AllowedProduct.Any(ap => iWant.Contains(ap.ProductId)))
.ToListAsync(ct);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.