如何使用Linq连接对象和实体? [英] How can I use Linq to join between objects and entities?
问题描述
在SQL中,我可以编写一个内存中的ID集合,我想从仅匹配这些ID的数据块中获取行。查询像 SELECT * FROM mytable WHERE id IN(1,3,5,10)
或在表之间加入。
我的问题是,EF无法使用本地数组或列表加入我的EF数据的查询。
(我使用的是EF4.1,但是我猜这个问题/解决方案在旧版本以及Linq-to-SQL中将是类似的。 )
您可以使用 Contains()
与您的ID集合 myIDs
生成等价的 WHERE id IN ..
查询:
var results = context.mytable.Where(x => myIds.Contains(x.Id));
I have a collection of IDs in memory, and I would like to fetch only rows from a DB matching those IDs.
In SQL, I could either write a query like SELECT * FROM mytable WHERE id IN (1,3,5,10)
or do a join between tables.
My problem is that EF can't build a query where I join my EF-data with my local array or list.
(I'm using EF4.1, but I'm guessing the problem/solution would be similar in older versions, as well as with Linq-to-SQL.)
You can use Contains()
with your ID collection myIDs
to generate the equivalent WHERE id IN ..
query:
var results = context.mytable.Where(x => myIds.Contains(x.Id));
这篇关于如何使用Linq连接对象和实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!