Entity framework 4.1 property with logic for use in a request

The problem is this, I have a property in essence:

public bool Expired { get { return CreationDate.AddDays(30) < DateTime.UtcNow; } } 

And, of course, I can’t use it in requests for entity infrastructure, although it does not contain anything that EF could not handle, it just doesn’t even try.

Question: is it possible to save such mini-queries as properties in any way in the entity (for example, using Func<T, bool> ) for reuse in the formation of more complex queries, for example

 Products.Where(x.Expired || x.Price > something) 

UPDATE: Oh, after posting the question, I tried a few more queries on google and found this:

http://damieng.com/blog/2009/06/24/client-side-properties-and-any-remote-linq-provider

This requires some additional code, so I wrote CompiledExpression using the specified library, like this for my property:

  private static readonly CompiledExpression<GuaranteeTransaction, bool> ShouldBeExpiredExpression = DefaultTranslationOf<GuaranteeTransaction> .Property(x => x.ShouldBeExpired) .Is(x => x.CreationDate < DateTime.UtcNow.AddDays(-30)); 

And it turns out that AddDays is also not supported by entity infrastructure. I think I could create another CompiledExpression to replace DateTime.UtcNow.AddDays (-30) with a property using the same mechanism, but that would be funny (in terms of readability and complexity).

+4
source share
1 answer

Until I got a better answer, I simplified things: I moved multiple expressions to the repository. It may not look so clean and maybe not in the best place, but they do their job and there is no additional complexity, so I think this is a pretty nice solution.

0
source

Source: https://habr.com/ru/post/1379269/


All Articles