Entity Framework - Common Query

心已入冬 提交于 2020-01-16 19:18:30

问题


referencing at previous post (Entity Wrapper - Custom) I still have some difficult about generic query to retrieving common field.

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 == "..."); 
}

But especially my issue is about impossibility to make casting from IQuerable where T : MyInterface to ObjectSet

Any suggestion wiil be appreciated..


回答1:


Maybe ObjectContext.CreateObjectSet Method could help you. As MSDN says, Method

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);
    }


来源:https://stackoverflow.com/questions/5515165/entity-framework-common-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!