如何从EF5 Code First中的Ids分隔列表中返回实体 [英] how do I return entities from delimited list of Ids in EF5 Code First

查看:100
本文介绍了如何从EF5 Code First中的Ids分隔列表中返回实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我以前在t中创建了一个表函数,我想通过传入一个逗号分隔的Ids列表来水合一个实体集合。 -sql,以逗号分隔的Ids列表传递,然后我将这个表加入到目标表中,并返回我的记录集。



什么是最优秀的使用EF5代码的方法首先实现相同?



更新:我想避免在内存中拥有完整的实体集。



Update2:我理想地喜欢实体的顺序来匹配分隔列表。

解决方案

我想说,通过将逗号分隔列表转换成包含您要进行的所有ID的列表< int> 开始。在这一点上,使用您特定的 DbContext 类,您将执行以下操作:

  var entities = db.MyEntities.Where(e => myListOfIds.Contains(e.ID))ToList(); 

注意:我只将 ToList 结束那里,因为你在谈论保湿收集。否则,使用IEnumerable,将会延迟执行查询,因此不会立即填充。


I want to hydrate a collection of entities by passing in a comma delimited list of Ids using EF5 Code First.

I would previously have created a table function in t-sql, passed in the comma delimited list of Ids, I'd then join this table to the target table and return my collection of records.

What is the most performant way of using EF5 Code First to achieve the same?

Update: I want to avoid having the full set of entities in memory first.

Update2: I'd ideally like the order of the entities to match the delimited list.

解决方案

I'd say to start out by converting the comma delimited list into a List<int> that contains all of the IDs that you would be going for. At that point, using your particular DbContext class you would do the following:

var entities = db.MyEntities.Where(e => myListOfIds.Contains(e.ID)).ToList();

Note: I only put the ToList at the end there because you were talking about hydrating the collection. Otherwise, with IEnumerable, there will be deferred execution of the query, and so it will not populate right away.

这篇关于如何从EF5 Code First中的Ids分隔列表中返回实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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