2016-11-10 25 views
1

我有以下存儲庫方法,我試圖將LINQ查詢ToList結果轉換爲無效的返回類型。任何意見,將不勝感激。如何將匿名對象的LINQ查詢通用列表轉換爲IEnumerable <object>

public class ProjectRepository : IProjectRepository 
{ 
    IDBEntities DBContext; 
    public ProjectRepository(IDBEntities db) 
    { 
     DBContext = db; 
    } 
    public IEnumerable<project> SelectAll(bool? is_debug_mode, int? performed_by_id) 
    { 
     var projects = (from p in DBContext.projects 
         join o in DBContext.organizations on p.organization_id equals o.organization_id 
         join m in DBContext.members on o.organization_id equals m.organization_id 
         where m.member_id == performed_by_id 
         select new 
         { 
          p 
         }).ToList(); 
     return (IEnumerable<project>)projects; 
    } 
} 

我收到的錯誤是:

無法轉換 類型的對象 'System.Collections.Generic.List 1[<>f__AnonymousType2 1 [NameSpace.Data.project]]' 鍵入 ' System.Collections.Generic.IEnumerable`1 [NameSpace.Data.project]」。

+5

應該是'選擇p' – Nkosi

回答

2

假設在LINQ查詢p的類型是project

var projects = (from p in DBContext.projects 
       join o in DBContext.organizations on p.organization_id equals o.organization_id 
       join m in DBContext.members on o.organization_id equals m.organization_id 
       where m.member_id == performed_by_id 
       select p).ToList(); 
return projects; 
+0

非常感謝您對這個,那個做了的。我對於廣泛使用LINQ是新手,並且在這個問題上停留了好幾個小時。 –

相關問題