LINQ to Entities does not recognize the method ToString()

LINQ to Entities does not recognize the method ToString()

Postby drysg » Thu 22 Mar 2012 18:46

I am getting the following error.

$exception {"LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression."} System.Exception {System.NotSupportedException}

The Look at the commented lines below. If I uncomment them then I get the error:
Code: Select all
            GPLModel.GPLEntities context = new GPLModel.GPLEntities();
            var matches = context.Viewbostons.Select(item => new CatalogEntry
                {
                    idx = item.Idx,
                    product = item.Product,
                    size = (long)item.SizeBytes,
                    //date = item.Date.ToString(),
                    type = item.Type,
                    classification = item.Classification,
                    distributor = item.Distributor,
                    //egplDate = item.EgplDate.ToString(),
                    classificationVal = (int)item.ClassificationInt,
                    handling = item.Handling,
                    creator = item.Creator,
                    datum = item.Datum,
                    elevation = (int)item.ElevationFt,
                    description = item.Description,
                    dirLocation = item.DoLocation,
                    bbox = item.Bbox,
                    uniqID = item.UniqId
                });

            CatalogEntry[] results = matches.ToArray();


Note that LinqConnect is declaring both date fields as System.DateTime?

(nullable) so I have to convert them to strings for my WCF connection.
drysg
 
Posts: 28
Joined: Fri 09 Mar 2012 15:10

Postby Shalex » Tue 27 Mar 2012 13:59

LINQ to Entities doesn't support .ToString(). So we recommend you to do the convertation at the Linq to Objects side: the first .Select() uses an anonymous type with a list of all necessary properties -> materialization (.ToArray()) -> the second .Select() with a new CatalogEntry (now you can call .ToString()).
Try using this code:
Code: Select all
        GPLModel.GPLEntities context = new GPLModel.GPLEntities();
        var matches = context.Viewbostons.Select(viewboston => new {
            viewboston.Idx,
            viewboston.Product,
            viewboston.SizeBytes,
            viewboston.Date,
            viewboston.Type,
            viewboston.Classification,
            viewboston.Distributor,
            viewboston.EgplDate,
            viewboston.ClassificationInt,
            viewboston.Handling,
            viewboston.Creator,
            viewboston.Datum,
            viewboston.ElevationFt,
            viewboston.Description,
            viewboston.DoLocation,
            viewboston.Bbox,
            viewboston.UniqId
        }).ToArray().Select(item => new CatalogEntry {
            idx = item.Idx,
            product = item.Product,
            size = (long)item.SizeBytes,
            date = item.Date.ToString(),
            type = item.Type,
            classification = item.Classification,
            distributor = item.Distributor,
            egplDate = item.EgplDate.ToString(),
            classificationVal = (int)item.ClassificationInt,
            handling = item.Handling,
            creator = item.Creator,
            datum = item.Datum,
            elevation = (int)item.ElevationFt,
            description = item.Description,
            dirLocation = item.DoLocation,
            bbox = item.Bbox,
            uniqID = item.UniqId
        });
Shalex
Devart Team
 
Posts: 7654
Joined: Thu 14 Aug 2008 12:44

Works

Postby drysg » Tue 27 Mar 2012 15:24

Thank you, this works. Consider adding the fact:

LINQ to Entities doesn't support .ToString().

to your documentation.

:D
drysg
 
Posts: 28
Joined: Fri 09 Mar 2012 15:10


Return to dotConnect for PostgreSQL