简体   繁体   中英

How does one use a custom property in a LINQ-to-Entities query?

I have a class Post which is an Entity Framework model. It contains a property like this:

public bool Showable {
  get {
    return this.Public && this.PublishedDate > DateTime.now
  }
}

I can use it in a query like this:

from p in db.Posts where p.Showable select p;

but when I have a property that uses it, like this

public IEnumerable<Post> ShowablePosts {
  get {
    return from p in db.Posts where p.Showable select p;
  }
}

then I can't do:

from p in ShowablePosts where p.Id > 42 select p;

It says:

The specified type member 'Showable' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

You can do this if you write the property as an Expression which is translatable to SQL.

Here's how to do it.

That's a bit complicated, because it's a general solution to a complicated problem.

The general idea is this: LINQ to Entities (like all LINQ providers) can't translate compiled code like your property into SQL at runtime. LINQ to Objects can execute compiled code, but it can't translate it. But they can translate an Expression<T> . So you could write:

public static Expression<Func<Post, bool>> WhereActive
{
    get
    {
        return p => p.Public && p.PublishedDate > DateTime.Now;
    }
}

Then you could write:

public IEnumerable<Post> ShowablePosts 
{
    get 
    {
        return db.Posts.Where(WhereActive);
    }
}

...and LINQ to Entities could translate that. The code in the post I link generalizes this idea.

遗憾的是,实体框架根本不支持计算属性(也就是说,返回计算值而不是对支持字段的引用的属性),但可能在将来的日期支持它。

我认为最简单的方法是使用本答案中提到的DelegateDecompiler.EntityFramework包的Computed属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM