[英]Remove item from IQueryable from List
I have 2 tables in my database Products
and PremiumProducts
for an ASP Net application.我的数据库
Products
和PremiumProducts
中有 2 个表用于 ASP Net 应用程序。
I retrieve products with a simple query我用一个简单的查询检索产品
IQueryable<Product> query = from p in myDataContext.Products select p;
I have a List of PremiumProducts
(which contains the ID of a Product
).我有一个
PremiumProducts
列表(其中包含Product
的 ID)。 I am attempting to remove the IDs in the PremiumProducts
list from the query above but not sure if there is a simple way or if i need to convert all the PremiumProducts
to a Product
first?我试图从上面的查询中删除
PremiumProducts
列表中的 ID,但不确定是否有简单的方法,或者我是否需要先将所有PremiumProducts
转换为Product
? I attempted this我试过这个
List<PremiumProducts> premiumProducts;
query = from p in query where (premiumProducts.Contains(p.ID)) select p;
but of course this brings back all sorts of casting errors.但这当然会带来各种铸造错误。
Is there a simple way to remove the PremiumProducts
from the query
above or do i need to convert the PremiumProducts
to a Product
, store the ID and then attempt to remove the query with these IDs?有没有一种简单的方法可以从上面的
query
中删除PremiumProducts
,或者我是否需要将PremiumProducts
转换为Product
,存储 ID,然后尝试使用这些 ID 删除查询?
Rather than:而不是:
query = from p in query where (premiumProducts.Contains(p.ID)) select p;
I would suggest:我会建议:
query = from p in query where (!premiumProducts.Select(z => z.ID).Contains(p.ID)) select p;
The key differences:主要区别:
!
!
(since you want to exclude those that are in premiumProducts
). premiumProducts
)。premiumProducts.Select(z => z.ID)
to ensure that the contains is against the ID
not the object (just avoiding your casting issues).premiumProducts.Select(z => z.ID)
来确保包含是针对ID
而不是 object (只是避免您的铸造问题)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.