EF cannot use time to convert string, and ef cannot convert string

Source: Internet
Author: User

EF cannot use time to convert string, and ef cannot convert string

Scenario:

The query condition must be time-formatted, for example, ToString ("yyyy-MM-dd;

In this case, use the following method:

Var q = from c in context. HasDateModels where c. UserDate. ToString ("yyyy-MM-dd"). Contains ("20") select c;

For query and use;

Tip: The method "System. String ToString (System. String)" is not recognized by LINQ to Entities. Therefore, this method cannot be converted to a storage expression.

That is, Linq to Entities does not support time String Conversion with Parameters

 

Solution:

The following method is applicable only when the data volume is small. We also recommend that you use this method. If there are other conditions, we recommend that you filter the data by using the following method;

Var q = from c in context. HasDateModels. AsEnumerable () where c. UserDate. ToString ("yyyy-MM-dd"). Contains ("20") select c;

Principle: Use AsEnumerable to convert the usage method to Linq to Object, and query the data to the memory before filtering.

Related Article

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.