2013-01-08 28 views
9

Làm cách nào tôi có thể tái cấu trúc LINQ này để thực hiện công việc này?LINQ to Entities không nhận ra phương thức 'System.String Format

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

bây giờ tôi nhận được lỗi:

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.

Tôi đã vô ích thử:

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

Trả lời

17

Bạn có thể làm string.Format trở lại trong LINQ-to-Đối tượng:

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

nên không thực sự được gọi ' ToList' để ** chắc chắn ** rằng truy vấn thực hiện trước? – James

+1

@James không cần; 'AsEnumerable()' phá vỡ thành phần 'IQueryable <>', đó là tất cả những gì cần thiết. –

+0

Tôi có thể sai, nhưng sẽ không IEnumerable gây ra * toàn bộ * 'mamDB.Apps' bảng được kéo vào bộ nhớ và sau đó áp dụng mệnh đề' where'? – James

0

Hãy thử điều này

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

không thay đổi bất cứ điều gì AFAIK, vì EF vẫn sẽ cố gắng xử lý truy vấn được soạn ra –

+1

Vì vậy, tôi đoán bạn thêm AsAnumerable như trong câu trả lời của bạn. – Dennis

+0

chính xác; đó là những gì AsEnumerable() sửa chữa –

Các vấn đề liên quan