Elad Benda
Elad Benda

Reputation: 36656

LINQ to Entities does not recognize the method 'System.String Format

How can i refactor this LINQ to make this work?

var a =  (from app in mamDB.Apps where app.IsDeleted == false 
                select string.Format("{0}{1}",app.AppName, 
                app.AppsData.IsExperimental? " (exp)": string.Empty))
               .ToArray();}

I now get error:

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

I have uselessly tried:

return (from app in mamDB.Apps where app.IsDeleted == false 
        select new string(app.AppName + (app.AppsData != null && 
        app.AppsData.IsExperimental)? " (exp)": string.Empty)).ToArray();

Upvotes: 9

Views: 10267

Answers (2)

Dennis
Dennis

Reputation: 2665

Try this

var a =  (from app in mamDB.Apps where app.IsDeleted == false 
            select new {AppName = app.AppName, IsExperimental = app.AppsData.IsExperimental})
          .Select(app => string.Format("{0}{1}",app.AppName, 
            app.IsExperimental? " (exp)": string.Empty))
           .ToArray();}

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062494

You can do the string.Format back in LINQ-to-Objects:

var a =  (from app in mamDB.Apps where app.IsDeleted == false 
         select new {app.AppName, app.AppsData.IsExperimental})
         .AsEnumerable()
         .Select(row => string.Format("{0}{1}",
            row.AppName, row.IsExperimental ? " (exp)" : "")).ToArray();

Upvotes: 17

Related Questions