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

时间:2013-02-12 13:46:00

标签: c# .net asp.net-mvc entity-framework

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

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

我有点惊讶,因为我能够在Linq2SQL中做到这一点,但不能在Entity Framework中做到这一点。

我可以请求任何帮助重写下面的代码吗?我已经看到了一些与此错误相关的示例,但我找不到特定于此方案的内容。感谢

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

2 个答案:

答案 0 :(得分: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:

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

答案 1 :(得分: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();