linq对实体不认识的方法 [英] linq to entities does not recognize the method

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

问题描述

我有一个存储库类,它有一个返回ObjectSet的方法

I have a repository class that has a method to return ObjectSet

public class Repository:ObjectContext
{
  public IObjectSet<T> GetObjectSet()
  {...}
}

当我使用在一个查询中,我得到linq到实体错误(不认识方法...)

When I use this in a query I get linq to entities error ( does not recognize method...)

我喜欢这样的想法,有一个通用存储库,每次都不会重新生成(与T4模板)
有没有办法扩展查询提供程序来识别我的方法?但是我想在服务器上解决它(换句话说就是参与查询生成)。
使用它的示例代码是:

I like this idea of having a generic repository which doesn't get regenerated every time (with T4 templates) Is there a way to extend the query provider to recognize my method ? But I do want to resolve it on server ( in other words participate in query generation). Sample code that uses it is:

static readonly Func<Repository,
        string, AuthorizedUser>  getInstitutionByAuthorizedUserName = CompiledQuery.Compile(
          (ObjectContext ctx, string userName) =>
              (from inst in ctx.GetObjectSet<Institution>()
              join auths in ctx.GetObjectSet<AuthorizedUser>() 
              on inst.Key equals auths.Institution.Key
              where auths.Name == userName
              select inst.Key).SingleOrDefault();


推荐答案

不要重新发明 CreateObjectSet break the join 习惯

Don't reinvent CreateObjectSet and break the join habit:

static readonly Func<Repository,
    string, AuthorizedUser>  getInstitutionByAuthorizedUserName = CompiledQuery.Compile(
      (ObjectContext ctx, string userName) =>
          from inst in ctx.CreateObjectSet<Institution>()
          from auths in inst.Authorizations
          where auths.Name == userName
          select inst.Key).SingleOrDefault();
       );

这篇关于linq对实体不认识的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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