实体框架列表包含在 lambda [英] Entity Framework List Contains in lambda

查看:22
本文介绍了实体框架列表包含在 lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用特定 ID 查询项目.例如:

I want to query items with specific IDs using. For example:

var ids = new List<int> { 1, 3, 5 };

var items = context.Items.Where(item => ids.Contains(item.ID)).ToList();

问题:

  1. 这是否会使用 SQL IN 运算符生成单个查询?
  2. 这段代码在性能方面可以吗?
  3. 有没有更好的方法?
  1. Will this generate a single query with SQL IN operator?
  2. Is this code OK in terms of performance?
  3. Are there any better ways to do it?

我正在使用带有 Microsoft SQL Server 的 Entity Framework 6.

I am using Entity Framework 6 with Microsoft SQL Server.

推荐答案

  1. 这会使用 SQL IN 运算符生成单个查询吗?
    是的
  2. 这段代码在性能方面可以吗?
    是(适用于小型列表)
  3. 有没有更好的方法?
    否(对于小型列表)

如果列表真的很大并且表相当小,您可能会获得更好的性能,将完整的表放入内存并与列表进行内存连接.

If the list is really big and the table is reasonably small you might get better performance bringing the complete table into memory and do an in memory join with the list.

这篇关于实体框架列表包含在 lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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