如何将匿名对象的LINQ查询通用列表转换为IEnumerable <object>

时间:2016-11-10 20:34:27

标签: c# .net entity-framework linq

我有以下存储库方法,我试图将LINQ查询ToList结果转换为我的返回类型,但没有用。任何建议将不胜感激。

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]'。

1 个答案:

答案 0 :(得分: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;