实体框架 - 常见查询 [英] Entity Framework - Common Query
问题描述
在上一篇文章中引用(实体包装 - 自定义)
我还有一些困难关于通用查询来检索公共字段。
referencing at previous post (Entity Wrapper - Custom) I still have some difficult about generic query to retrieving common field.
我有一个简单的界面,只有一个字段。我的所有实体从我的界面继承。然后我有一个封装我的objectContext类的类。
嗯,现在我需要执行一个linq查询来获取我的IQuerable对象。
以下片段在建立时间内出现错误:
I've a simple interface with one field only. All my entities inheritance from my interface. Then I've a class encapsulating my objectContext typed. Well, now I need to execute a linq query to get my IQuerable object. The following snippet goes on error at building time:
public IQueryable<T> GetQuery<T>() where T : IEntity
{
var query =
GetObjectSetSomehow; //problem: I don't know the objectSet type here!!
return query.Where(p => p.field == "...");
}
但特别是我的问题是不可能从IQuerable进行投射,其中T:MyInterface to ObjectSet
But especially my issue is about impossibility to make casting from IQuerable where T : MyInterface to ObjectSet
任何建议都将被赞赏..
Any suggestion wiil be appreciated..
推荐答案
也许 ObjectContext.CreateObjectSet方法可以帮助您。正如MSDN所说,方法
Maybe ObjectContext.CreateObjectSet Method could help you. As MSDN says, Method
创建一个新的ObjectSet
实例,用于查询,添加
modify,并删除
指定的实体类型的对象。
Creates a new ObjectSet instance that is used to query, add, modify, and delete objects of the specified entity type.
public static IQueryable<T> Create<T>(ObjectContext context) where T : class, IEntity
{
var query = context.CreateObjectSet<T>().AsQueryable();
return query.Where(x => true);
}
这篇关于实体框架 - 常见查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!