EF architecture ~ Use date functions in LINQ to entity

Source: Internet
Author: User

Back to directory

Keywords, such as trim, substring, and totaldays, cannot appear in the query statement of LINQ to entity. net keywords are not supported in EF queries. The reason may be to better improve the query performance. After all, good performance depends on your program standards, with a strict standard, a good program can be designed.

Today, we will mainly talk about a backdoor left by EF for the date method. <backdoor> this word should be known in Chinese society. As the name suggests, it is to go against the principle, your principles are useless to me, haha! This is sometimes useful, because under the principle of big, it is very likely that some requirements cannot be implemented. In this case, a backdoor is needed!

The following categories in EF:

System. Data. Objects. sqlclient. sqlfunctions

Let's take an example. This example involves a time-based query.

   public System.Linq.Expressions.Expression<Func<Classroom_Live, bool>> SatisfiedBy()    {            Specification<Classroom_Live> spec = new TrueSpecification<Classroom_Live>();            if (_status.HasValue)            {                if (_status.Value == 0)                    spec &= new DirectSpecification<Classroom_Live>(o => (o.Status & (int)Status.AuditNotPass) > 0                        || (o.Status & (int)Status.Auditing) > 0                        || DateTime.Now < o.BeginTime);                else if (_status == 1)                    spec &= new DirectSpecification<Classroom_Live>(o => (o.Status & (int)Status.AuditPass) > 0                        && System.Data.Objects.SqlClient.SqlFunctions.DateDiff("mi", DateTime.Now, o.BeginTime) < o.Minutes);                else                    spec &= new DirectSpecification<Classroom_Live>(o => (o.Status & (int)Status.AuditPass) > 0                        && System.Data.Objects.SqlClient.SqlFunctions.DateDiff("mi", DateTime.Now, o.BeginTime) > o.Minutes);            }            return spec.SatisfiedBy();    }

Back to directory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.