【问题标题】:How to convert LINQ Query Generic List of Anonymous objects to IEnumerable<object>如何将匿名对象的 LINQ 查询通用列表转换为 IEnumerable<object>
【发布时间】:2016-11-10 20:34:26
【问题描述】:

我有以下存储库方法,我试图将 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.List1[&lt;&gt;f__AnonymousType21[NameSpace.Data.project]]' 输入 'System.Collections.Generic.IEnumerable`1[NameSpace.Data.project]'。

【问题讨论】:

  • 应该是select p

标签: c# .net entity-framework linq


【解决方案1】:

假设 linq 查询中的 pproject 类型

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;

【讨论】:

  • 非常感谢你,做到了。我不熟悉广泛使用 LINQ,并且已经坚持了几个小时。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-27
  • 1970-01-01
  • 1970-01-01
  • 2016-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多