2013-01-08 5 views
9

可能System.String」メソッドを認識しませんか?は、私がこの仕事をするために、この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 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.

私が無駄に試してみました:

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

http://stackoverflow.com/questions/10079990/linq-to-entities-does-not-recognize-the-method-system-string-formatsystem-stri – Thelonias

答えて

17

あなたはLINQツーオブジェクトに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(); 
+0

は、あなたが本当に 'を呼び出すしてはいけませんToList'を実行して**クエリが最初に実行されることを確認します**。 – James

+1

@ジェームスは必要ありません。 'AsEnumerable()'は 'IQueryable <>'コンポジションを中断します。これはすべて必要です。 –

+0

は私が間違っているかもしれないが、IEnumerableを原因は、*全体* 'mamDB.Apps'テーブルはメモリ内に引き込まれないようになり、その後、' WHERE'句を適用しますか? – James

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

。 –

+1

を処理しようとするので、私の知る限り何も変わりません – Dennis

+0

;それはAsEnumerable()が修正するものです –

関連する問題