问题描述
我的大脑现在似乎已经变得糊涂了!我正在使用LINQ to Entity,我需要从一个表中获取一些数据,而该数据在另一表中不存在.
My brain seems to be mush right now! I am using LINQ to Entity, and I need to get some data from one table that does NOT exist in another table.
例如:我需要表A中的groupID,groupname和groupnumber,它们在表B中不存在.groupID将与其他相关信息一起存在于表B中.这些表没有任何关系.在SQL中,它非常简单(有一个更优雅,更有效的解决方案,但我想画一幅所需的图片)
For example: I need the groupID, groupname and groupnumber from TABLE A where they do not exist in TABLE B. The groupID will exist in TABLE B, along with other relevant information. The tables do not have any relationship. In SQL it would be quite simply (there is a more elegant and efficient solution, but I want to paint a picture of what I need)
SELECT
GroupID,
GroupName,
GroupNumber,
FROM
TableA
WHERE
GroupID NOT IN (SELECT GroupID FROM TableB)
是否有使用Entity Framework/LINQ to Entity的简便/优雅方法?现在,我有很多查询进入数据库,然后进行比较,等等.这很混乱.
Is there an easy/elegant way to do this using the Entity Framework/LINQ to Entity?Right now I have a bunch of queries hitting the db, then comparing, etc. It's pretty messy.
推荐答案
您可以使用任何
var temp =context.TableA
.Where(x=>!context.TableB.Any(y=>y.GroupID!=x.GroupID))
.Select(x=>new { GroupID = x.GroupID, GroupName=x.GroupName, GroupNumber = x.GroupNumber}).ToList();
这篇关于LINQ to Entity,在NOT IN表上联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!