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

    Question

  • I'm getting the following error:

     

    System.NotSupportedException: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression

     

    Code Snippet

    list =

    (from ...

    select new FactProjectTeamMonthEntity

    {

    ProjectTeamId = pt.ProjectTeamId,

    YearId = tym.YearId,

    MonthId = tym.MonthId,

    YearMonthName = ReturnDimensionInfo ? tym.YearId.ToString() + "-" + tym.TimeMonth.Name : null,

    FteActualCount = fpm.FteActualCount

    }).ToList();

     

     

    This is very surprising.  It would seem that ToString would be one of the easier methods to translate (and LINQ to SQL handles it just fine).

     

    Any recommended workaround?

     

    -Larry

     

    Thursday, August 28, 2008 7:09 PM

Answers

  • I suppose LINQ to Objects is one possibility:

     

    Code Snippet

    var q =

    (from ...

    select new

    {

    ProjectTeamId = pt.ProjectTeamId,

    YearId = tym.YearId,

    MonthId = tym.MonthId,

    MonthName = tym.TimeMonth.Name,

    FteActualCount = fpm.FteActualCount

    });

     

    list =

    (from x in q.AsEnumerable()

    select new FactProjectTeamMonthEntity

    {

    ProjectTeamId = x.ProjectTeamId,

    YearId = x.YearId,

    MonthId = x.MonthId,

    YearMonthName = ReturnDimensionInfo ? x.YearId.ToString() + "-" + x.MonthName : null,

    FteActualCount = x.FteActualCount

    }).ToList();

     

     

     

    Thursday, August 28, 2008 7:17 PM

All replies

  • I suppose LINQ to Objects is one possibility:

     

    Code Snippet

    var q =

    (from ...

    select new

    {

    ProjectTeamId = pt.ProjectTeamId,

    YearId = tym.YearId,

    MonthId = tym.MonthId,

    MonthName = tym.TimeMonth.Name,

    FteActualCount = fpm.FteActualCount

    });

     

    list =

    (from x in q.AsEnumerable()

    select new FactProjectTeamMonthEntity

    {

    ProjectTeamId = x.ProjectTeamId,

    YearId = x.YearId,

    MonthId = x.MonthId,

    YearMonthName = ReturnDimensionInfo ? x.YearId.ToString() + "-" + x.MonthName : null,

    FteActualCount = x.FteActualCount

    }).ToList();

     

     

     

    Thursday, August 28, 2008 7:17 PM
  • I don't know about LINQ to SQL, but the EF draws a clear boundary between what happens on the client and what happens on the server, and LINQ expressions get evaluated on the server. Given that ToString() could do anything at all, including sending mail to your boss, it means that ToString() is not supported.

     

    Now that said, what happens if you just drop the ToString() from your query? E.g. ... tym.YearId + "-" + ...

    Thursday, August 28, 2008 7:21 PM
    Moderator
  • I think that ? : isn't recognize by LINQ To EF too.

    Thursday, August 28, 2008 8:20 PM
  • I was surprised that I could do tym.YearId + "-" + ... but I can.  I thought I would get a compilation error.  I assume the compiler automatically casts tym.YearId to a string?

     

    Anyway, when I drop ToString() I get this error:

     

    System.NotSupportedException: Unable to cast the type 'System.Int32' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types

     

    I guess using LINQ to Objects for some post-processing is my best workaround?

     

    -Larry

     

    Friday, August 29, 2008 11:59 AM
  • This topic contains a list of CLR methods that can be converted to command tree canonical functions and executed on the server:

     

    http://msdn.microsoft.com/en-us/library/bb738681.aspx

     

    For CLR methods not on this list, you would have to pull the results down to the client using .AsEnumerable() and execute a LINQ to Objects query (similar to Lawerence's code snippet above).

     

    -Ryan

     

    Wednesday, September 24, 2008 4:46 PM
  • I had the same prob. I edited the code as

     

     

    string yearToString = tym.YearId.ToString();

    and

    YearMonthName = ReturnDimensionInfo ? yearToString + "-" + tym.TimeMonth.Name : null,

    It worked for me.

    Tuesday, April 06, 2010 5:53 AM
  • While this is clearly a limitation, I actually like it as it forces a cleaner coding practice. Here's an example:

    This fails:

    AreaCode = context.AreaCodeLookUps.Where(o => o.AreaName.ToUpper().Equals(form["AreaName"].ToString().ToUpper())).FirstOrDefault<AreaCodeLookUp>().AreaCode;

    while this (which is more readable and cleaner) succeeds: 

    string helper = form["AreaName"].ToString().ToUpper();

    AreaCode = context.AreaCodeLookUps.Where(o => o.AreaName.ToUpper().Equals(helper)).FirstOrDefault<AreaCodeLookUp>().AreaCode;

    n.o.



    Friday, July 29, 2011 10:03 PM
  • I agree this is the right model for complex formatted strings. But what about ordinary string comparison?
    On the store, the correct expression is

    WHERE a = b

    In C#, the equivalent expression is

    StringComparer.OrdinalIgnoreCase.Compare(a,b)

    but this fails.
    When I replace it with

    a == b

    the code executes, but I don't know whether I am getting the right value.

    Tuesday, March 13, 2012 9:12 PM
  • prefer C# but customer required I do project in VB.NET
    I called ToList() before Concat and that did the trick

    Dim query = From c In Context.tbl_Station
    Dim query2 = From x In query.ToList() Select StationID = x.StationID, StationName = ("(V" & x.VisnID & ")(" & x.StationNumber + ")-") & x.OfficialStationName
    Thursday, June 20, 2013 11:44 PM