9

我如何重构这个 LINQ 以使其工作?

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();}

我现在得到错误:

LINQ to Entities 无法识别方法 'System.String Format(System.String, System.Object, System.Object)' 方法,并且此方法无法转换为存储表达式。

我徒劳地尝试过:

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();
4

2 回答 2

17

您可以string.Format在 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();
于 2013-01-08T13:28:33.127 回答
0

尝试这个

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();}
于 2013-01-08T13:33:25.490 回答