获取所有除使用Entity Framework的SQL数据库 [英] Get All Except from SQL database using Entity Framework

查看:145
本文介绍了获取所有除使用Entity Framework的SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的产品列表

var r = db.Products.Where(x => x.Sites
                                .Where(z => z.Key == associatedProducts.Key)
                                .Any()
                  ).ToList()

有一个实体,称为产品,我想从除产品的所有元素associatedProducts.Products存在

There is an entity called Products, I want to get all elements from products except those exist in associatedProducts.Products

我怎样才能做到这一点?

How can i do that ?

推荐答案

下面的查询工作如果associatedProducts名单是牵强的previos查询中使用EF

The following query works if associatedProducts list is fetched using EF in a previos query.

var temp = db.Products.ToList().Except(associatedProducts).ToList();



否则,如果 associatedProducts 是一个列表,其中一直没有取出使用EF(假设是一个整数);

otherwise, if associatedProducts is a list which has not been fetched using EF (assuming Key is an integer);

List<int> tempIdList = associatedProducts.Select(q => q.Key ).ToList();
var temp = db.Products.Where(q => !tempIdList.Contains(q.Key));

这篇关于获取所有除使用Entity Framework的SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆