实体框架:从列表中的ids获取表中的所有行 [英] Entity Framework: Get all rows from the table for the ids in list

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

问题描述

我的情况是我有ids {2,10,16,24,32,...},并希望从表中获取与这些ids匹配的行。如何在Entity框架中执行。

My situation is I have ids {2,10,16,24,32,...} and would like to get the rows that matches these ids from the table. How do I do it in Entity framework.

在SQL中,我可以执行以下操作:

In SQL I can do something like:


SELECT * FROM table WHERE id IN(2,10,16,24,32)

SELECT * FROM table WHERE id IN (2,10,16,24,32)

实体框架?

推荐答案

您可以将您的ids插入列表中,并在Where中使用它来过滤掉表中的行其ID与列表中的ID匹配:

You can shove your ids into a list and use that inside the Where to filter out only the rows in table whose id matches those in the list:

var ids = new List<int>() { 2, 10, 16, 24, 32 };
var rows = Table.Where(t => ids.Contains(t.id)).ToList();

这篇关于实体框架:从列表中的ids获取表中的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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