如何使用EF和C#编写通用查询 [英] how can i write a generic query with EF and C#

查看:59
本文介绍了如何使用EF和C#编写通用查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在这样的实体框架中编写查询:

  T实体=(来自myContext.typeof(T)中的e) 

我尝试使用字符串concat,但这不适用于从 myContext

获得成员

更多代码:

  public T GetEntity< T>(Guid实体ID,字符串connectionString){使用(RContext rpContext = new RContext(connectionString)){var实体=(来自rpContext.Set< T>()中的e其中e.Id == entityId选择e).FirstOrDefault();返回(T)Convert.ChangeType(entity,typeof(T));}} 

解决方案

您可以像这样访问通用的 Set 方法:

  var实体=(来自myContext.Set< T>()中的e); 

更新:您将需要向您的方法添加通用类型约束,以确保通用类型 T 将匹配已应用于方法 DbContext.Set< T> 的约束.

更新2:您不需要强制转换您的实体;它已经是 typeof(T).

  public T GetEntity< T>(Guid实体ID,字符串connectionString)其中T:类{使用(RContext rpContext = new RContext(connectionString)){返回(从rpContext.Set< T>()中的e返回其中e.Id == entityId选择e).FirstOrDefault();}} 

更新3:假定您未附加到linq语法,则可以直接将谓词传递到 FirstOrDefault 方法中.

  public T GetEntity< T>(Guid实体ID,字符串connectionString)其中T:类{使用(RContext rpContext = new RContext(connectionString)){返回rpContext.Set< T>().FirstOrDefault(e => e.Id == entityId);}} 

更新4:在方法内部使用 Id 属性时,您还需要将方法约束为具有该属性的类型-通过约束公共接口或公共基类.如果它是基类,则可以删除 class 约束.

i want to write a query in entity framework like this:

T entity = (from e in myContext.typeof(T) )

i tried to use string concat but this dose not work for get a member from myContext

more code:

public T GetEntity<T>(Guid entityId, string connectionString)
{
    using (RContext rpContext = new RContext(connectionString))
    {

        var entity = (from e in rpContext.Set<T>()
                                    where e.Id == entityId
                                    select e).FirstOrDefault();
                    return (T) Convert.ChangeType(entity, typeof(T));

    }
}

解决方案

You can access the generic Set method like this:

var entities = (from e in myContext.Set<T>());

Update: You will need to add generic type constraints to your method, to ensure the generic type T will match the constraints already applied to the method DbContext.Set<T>.

Update 2: You don't need to cast your entity; it is already typeof(T).

public T GetEntity<T>(Guid entityId, string connectionString)
    where T : class
{
    using (RContext rpContext = new RContext(connectionString))
    {

        return (from e in rpContext.Set<T>()
                where e.Id == entityId
                select e).FirstOrDefault();
    }
}

Update 3: You can pass your predicate straight into the FirstOrDefault method, assuming you're not attached to the linq syntax.

public T GetEntity<T>(Guid entityId, string connectionString)
    where T : class
{
    using (RContext rpContext = new RContext(connectionString))
    {
        return rpContext.Set<T>().FirstOrDefault(e => e.Id == entityId);
    }
}

Update 4: As you're using the Id property inside your method, you will also need to constrain your method to types which have that property - either by constraining a common interface, or a common base class. If it's a base class, you can remove the class constraint.

这篇关于如何使用EF和C#编写通用查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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