EF-linq2entities无法识别.tostring()方法

本文关键字:方法 tostring 识别 EF-linq2entities | 更新日期: 2023-09-27 17:58:52

我无法编译以下代码,并且收到以下错误:

LINQ to Entities does not recognize the method 'System.String ToString()

我有点惊讶,因为我可以在Linq2SQL中做到这一点,但不能在实体框架中做到。

请帮我重写下面的代码好吗?我看到了一些与这个错误相关的例子,但我找不到特定于这个场景的东西。感谢

 using (ctx)
 {
                   var productResults = (from q in ctx.Products
                                         where q.ProductId == productId && q.Model == productModel
                                         select new Models.ProductDTO
                                         {
                                             Id = q.ProductId,
                                             Name = q.Name.ToString(),
                                             Year = q.Year.ToString("MMM ddd d HH:mm yyyy"),
                                             Model = q.Model,
                                             Description = q.Description.ToString()
                                         }).Distinct().ToList().AsParallel();
                   Department.Products = productResults;
                }

EF-linq2entities无法识别.tostring()方法

context 首次获取列表

var productResults = ctx.Products.where(q => q.ProductId == productId && q.Model == productModel).ToList();

然后查询为并选择新类型作为ProductDTO

var productDTO = (from q in productResults 
                 select new Models.ProductDTO
                 {
                      Id = q.ProductId,
                      Name = q.Name.ToString(),
                      Year = q.Year.ToString("MMM ddd d HH:mm yyyy"),
                      Model = q.Model,
                      Description = q.Description.ToString()
                 }).Distinct().ToList().AsParallel();

评论后

IEnumerable:

IEnumerable<Products> list = context.Products.Take(10);
// after this line data load the memory that fetched from DB.

SQL输出:

 Select * FROM Table

IQuerable:

IQuerable<Products> list = context.Products.Take(10);
// data still not fetch from DB

SQL输出:

 Select Top 10 FROM Table

您也可以在一个查询中执行此操作;

            var productResults = ctx.Products.Where(q => q.ProductId == productId && q.Model == productModel).ToList()
                .Select<Product, ProductDTO>(m =>
                {
                    Models.ProductDTO dto= new Models.ProductDTO();
                    dto.Id = m.ProductId;
                    dto.Name = m.Name.ToString();
                    dto.Year = m.Year.ToString("MMM ddd d HH:mm yyyy");
                    dto.Model = m.Model;
                    dto.Description = m.Description.ToString();
                    return dto;
                }).Distinct().ToList().AsParallel();

可能有更好的方法,但将其分解为两个查询可能会奏效。

var productResults = (from q in ctx.Products
     where q.ProductId == productId && q.Model == productModel
     select q).ToList();
var temp = from o in productResults
         select new Models.ProductDTO
         {
             Id = q.ProductId,
             Name = q.Name.ToString(),
             Year = q.Year.ToString("MMM ddd d HH:mm yyyy"),
             Model = q.Model,
             Description = q.Description.ToString()
         }).Distinct().ToList();