EF中的Join和Contains的区别是什么

2020-06-08 20:31发布

问题:

听说
List<int> lstBizIds = new List<int>() { 1, 2, 3, 4, 5 };
List<int> lstProjectIds = context.Projects.Where(x =>lstBizIds.Contains(x.businessId)).Select(x => x.projectId).ToList();
会翻译成sql 是 in
SELECT Id FROM ProjectsWHERE UserId IN (1, 2, 3, 4, 5, 6)

List<int> lstBizIds = new List<int>() { 1, 2, 3, 4, 5 };
List<int> lstProjectIds = context.Projects.Join(lstBizIds, p => p.businessId, u => u, (p, u) => p.projectId).ToList();
会翻译成sql 是
SELECT Id
FROM Projects
INNER JOIN (VALUES (1), (2), (3), (4), (5)) AS Data(Item) ON Projects.UserId = Data.Item

但是我用join的时候,sql server 监听数据库的sql语句时,join反而是
SELECT Id FROM Projects 的
而且在使用ToListAsync等异步方法的时候,Join性能很差,用Contains反而会很好。这是为什么呢

回答1:

上面的查询场景完全不需要用 Join



回答2:

就和sql里的 join和 like '%hello%'的区别



回答3:

Join:连接,返回的T where T is class;Contains:(是否)包含,返回 bool;



标签: ef join contains