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

查看:76
本文介绍了实体框架列表包含在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?

我正在将Entity Framework 6与Microsoft SQL Server一起使用.

I am using Entity Framework 6 with Microsoft SQL Server.

推荐答案

  1. 这会使用SQL IN运算符生成单个查询吗?
    是的
  2. 就性能而言,此代码行吗?
    是(对于小名单)
  3. 还有更好的方法吗?
    否(针对小名单)
  1. Will this generate a single query with SQL IN operator?
    Yes
  2. Is this code OK in terms of performance?
    Yes (for small lists)
  3. Are there any better ways to do it?
    No (for small lists)

如果列表确实很大而表相当小,则可能会获得更好的性能,将整个表放入内存并对该列表进行内存内连接.

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天全站免登陆