Page 1 of 1

LINQ to Entities does not recognize the method ToString()

Posted: Thu 22 Mar 2012 18:46
by drysg
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.

Posted: Tue 27 Mar 2012 13:59
by Shalex
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
        });

Works

Posted: Tue 27 Mar 2012 15:24
by drysg
Thank you, this works. Consider adding the fact:

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

to your documentation.

:D