Raoul_W
Raoul_W

Reputation: 11

NotSupportedException occured LINQ to Entities does not recognize the method ToList

I am working on a ASP.NET MVC 4.5 Application with an WCF back-end where entity framework 5 is used as Data-Access. PictureUse is the entity, PictureUsageContract is the DataContract that is transmitted through WCF.

The Repository.GetAll is a standard generic get all to get entities out of an Object Context.

var allPictureUses = _pictureUseRepository.GetAll()
                .Where(x => x.Name != null)
                .Include(x => x.PictureUse1)
                .Select(x => new PictureUsageContract()
                {
                    ID = x.ID,
                    DefaultPrice = x.DefaultPrice,
                    Name = x.Name,
                    UseDescription = x.UseDescription,
                    SubItemList = x.PictureUse1.Select(c => c.UseDescription).ToList()        

                }).ToList();

Exception recieved while debugging: {"LINQ to Entities does not recognize the method 'System.Collections.Generic.List1[System.String] ToList[String](System.Collections.Generic.IEnumerable1[System.String])' method, and this method cannot be translated into a store expression."}

I know it is the following line that did it:

  SubItemList = x.PictureUse1.Select(c => c.UseDescription).ToList()  

SubItemList is a List PictureUse is an Entity.

I searched a bit beforehand and know that it might be that .Select isn't properly supported by LINQ to Entities, but I have not found any concrete solution to this problem.

Any help would be welcome.

Upvotes: 1

Views: 1342

Answers (2)

Yann Olaf
Yann Olaf

Reputation: 597

Try

var allPictureUses = _pictureUseRepository.GetAll()
                .Where(x => x.Name != null)
                .Include(x => x.PictureUse1)

                .ToArray()

                .Select(x => new PictureUsageContract()
                {
                    ID = x.ID,
                    DefaultPrice = x.DefaultPrice,
                    Name = x.Name,
                    UseDescription = x.UseDescription,
                    SubItemList = x.PictureUse1.Select(c => c.UseDescription).ToList()        
            }).ToList();

Upvotes: 1

sgmoore
sgmoore

Reputation: 16067

If you are getting an error saying that Linq To Entities does not support ToList, then the obvious thing to do is to remove it.

So I would try

var allPictureUses = _pictureUseRepository.GetAll()
            .Where(x => x.Name != null)
            .Include(x => x.PictureUse1)
            .Select(x => new PictureUsageContract()
            {
                ID = x.ID,
                DefaultPrice = x.DefaultPrice,
                Name = x.Name,
                UseDescription = x.UseDescription,
                SubItemList = x.PictureUse1.Select(c => c.UseDescription)        

            }).ToList();

If that does not work, (eg it might not allow you to assign to SubItemList) then you might have to retrieve the details into a list of anonymous objects before converting them to a List

eg

var allPictureUses = _pictureUseRepository.GetAll()
            .Where(x => x.Name != null)
            .Include(x => x.PictureUse1)
            .Select(x => new 
            {
                ID = x.ID,
                DefaultPrice = x.DefaultPrice,
                Name = x.Name,
                UseDescription = x.UseDescription,
                SubItemList = x.PictureUse1.Select(c => c.UseDescription)        

            }).ToList()

            .Select(x => new PictureUsageContract()
            {
                ID = x.ID,
                DefaultPrice = x.DefaultPrice,
                Name = x.Name,
                UseDescription = x.UseDescription,
                SubItemList = x.SubItemList.ToList()     

            }).ToList();

Upvotes: 0

Related Questions