实体框架 - 常见查询 [英] Entity Framework - Common Query

查看:115
本文介绍了实体框架 - 常见查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上一篇文章中引用(实体包装 - 自定义
我还有一些困难关于通用查询来检索公共字段。

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屋!

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