LINQ to Entities不能识别'System方法.字符串格式

本文关键字:System 方法 字符串 格式 to Entities 不能 识别 LINQ | 更新日期: 2023-09-27 17:50:57

我如何重构这个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。字符串格式(系统。字符串,系统。Object, System.Object)'方法,以及this方法不能转换为存储表达式。

我徒劳地尝试了:

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

LINQ to Entities不能识别'System方法.字符串格式

您可以在LINQ-to-Objects中执行string.Format:

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

试试这个

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();}
相关文章: