Ren
Ren

Reputation: 1503

EF - linq2entities does not recognize .tostring() method?

I'm unable to compile the below code and I'm getting the following error:

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

I'm a bit surprised because I was able to do this in Linq2SQL but can't do it in Entity Framework.

Could I please get any help rewriting the below code ? I've seen a few examples related to this error but I couldn't find something that is specific to this scenario. Thanks

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

Upvotes: 1

Views: 184

Answers (2)

AliRıza Adıyahşi
AliRıza Adıyahşi

Reputation: 15866

First get list from context

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

Then query is and select new Type as 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();

AFTER COMMENT

IEnumerable:

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

SQL Output:

 Select * FROM Table

IQuerable:

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

SQL Output:

 Select Top 10 FROM Table

Upvotes: 1

daryal
daryal

Reputation: 14929

you may also do this in one query;

            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();

there may be a better way, but breaking it into two queries may work.

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();

Upvotes: 1

Related Questions