1

我无法编译以下代码,并且出现以下错误:

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;
                }
4

2 回答 2

1

首先从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<Products> list = context.Products.Take(10);
// after this line data load the memory that fetched from DB.

SQL 输出:

 Select * FROM Table

可查询:

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

SQL 输出:

 Select Top 10 FROM Table
于 2013-02-12T13:50:46.937 回答
1

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

            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();
于 2013-02-12T13:51:54.337 回答