c# - 将 IEnumerable 转换为 List 时出现问题

标签 c# linq generics ienumerable anonymous-types

我有一个方法GetAllReportGroups,其中我只是返回一个 IEnumerable,但在代码审查中我被告知不,更改它!

现在我创建了更多的类,我“认为”这个 ReportGroupMaster 将是合适的返回类型,我得到 这个错误。我不再使用 IEnumerable,所以它让我感到困惑。

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<> items, int Id, int SortOrder, int Type>>' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)

方法:

public List<ReportGroupMaster> GetAllReportGroups(string language)
    {

        List<ReportGroup> reportGroups = _envyUnitOfWork.ReportGroupsAll.GetAll().ToList();
        List<ReportDefinition> reportDefinition = _envyUnitOfWork.ReportDefinitions.GetAll().ToList();
        List<TraxReport> traxReports = _envyUnitOfWork.TraxReports.GetAll().ToList();

        List<ReportGroupItem> reportGroupItems = reportGroups.Select(reportGroup => new ReportGroupItem() {Id = reportGroup.Id, ReportGroupName = reportGroup.ReportGroupName, SortOrder = reportGroup.SortOrder, Type = (int) ReportTypeNames.ReportGroup}).ToList();

        //List<ReportGroupMaster> reportGroupItems = reportGroups.Select(reportGroup => new ReportGroupMaster() {Id = reportGroup.Id, ReportGroupName = reportGroup.ReportGroupName, SortOrder = reportGroup.SortOrder, Type = (int) ReportTypeNames.ReportGroup}).ToList();




       var query = from d in reportGroupItems
                    join r in reportDefinition on d.Id equals r.ReportGroupID into items
                    join cr in traxReports on d.Id equals cr.ReportGroupID into customItems
                    orderby d.SortOrder
                    select new
                    {
                        d.ReportGroupName,
                        items = items.Select(r => new
                        {
                            r.Id,
                            r.ReportGroupName,
                            r.SortOrder,
                            r.ReportGroupID,
                            r.Type// = (int) ReportTypeNames.ReportDefinition //r.Type
                        }).Concat(customItems.Select(cr => new
                        {
                            cr.Id,
                            cr.ReportGroupName,
                            cr.SortOrder,
                            cr.ReportGroupID,
                            cr.Type// = 4 //(int) ReportTypeNames.TraxReport    //cr.Type
                        })).OrderBy(e => e.SortOrder).ToList(),
                        d.Id,
                        d.SortOrder,
                        d.Type
                    };

        return query;

    }

有什么想法吗?

ReportGroupMaster

public class ReportGroupMaster
{
    public string ReportGroupName { get; set; }
    public List<ReportGroupChild> Items { get; set; }
    public int Id { get; set; }
    public int SortOrder { get; set; }
    public int Type { get; set; }
}

报表组子级

public class ReportGroupChild
{
    public int Id { get; set; }
    public string ReportGroupName { get; set; }
    public int SortOrder { get; set; }
    public int ReportGroupID { get; set; }
    public int Type { get; set; }
}

最佳答案

Linq您的情况的查询返回 IEnumerable<anonymoustype>但预期的返回类型是 List<ReportGroupMaster> ,这就是导致错误的原因。

使用此代码:

public List<ReportGroupMaster> GetAllReportGroups(string language)
{
    List<ReportGroup> reportGroups = _envyUnitOfWork.ReportGroupsAll.GetAll().ToList();
    List<ReportDefinition> reportDefinition = _envyUnitOfWork.ReportDefinitions.GetAll().ToList();
    List<TraxReport> traxReports = _envyUnitOfWork.TraxReports.GetAll().ToList();

    List<ReportGroupItem> reportGroupItems = reportGroups.Select(reportGroup => new ReportGroupItem() {Id = reportGroup.Id, ReportGroupName = reportGroup.ReportGroupName, SortOrder = reportGroup.SortOrder, Type = (int) ReportTypeNames.ReportGroup}).ToList();

    //List<ReportGroupMaster> reportGroupItems = reportGroups.Select(reportGroup => new ReportGroupMaster() {Id = reportGroup.Id, ReportGroupName = reportGroup.ReportGroupName, SortOrder = reportGroup.SortOrder, Type = (int) ReportTypeNames.ReportGroup}).ToList();

   var query = from d in reportGroupItems
                join r in reportDefinition on d.Id equals r.ReportGroupID into items
                join cr in traxReports on d.Id equals cr.ReportGroupID into customItems
                orderby d.SortOrder
                select new ReportGroupMaster()
                {
                    ReportGroupName  =d.ReportGroupName,
                    Items  = items.Select(r => new
                    {
                        r.Id,
                        r.ReportGroupName,
                        r.SortOrder,
                        r.ReportGroupID,
                        r.Type// = (int) ReportTypeNames.ReportDefinition //r.Type
                    }).Concat(customItems.Select(cr => new
                    {
                        cr.Id,
                        cr.ReportGroupName,
                        cr.SortOrder,
                        cr.ReportGroupID,
                        cr.Type// = 4 //(int) ReportTypeNames.TraxReport    //cr.Type
                    })).OrderBy(e => e.SortOrder).ToList(),
                    Id = d.Id,
                    SortOrder  = d.SortOrder,
                    Type = d.Type
                };

    return query.ToList();
}

关于c# - 将 IEnumerable 转换为 List 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36419243/

相关文章:

java - 需要使用泛型将列表过滤到特定的子类

c# - 对包含索引的多行进行分组并为每个索引创建自定义对象列表

c# - 同一行上的 Linq 更多条件

c# - 将异步等待集成到同步方法中

具有已知事件签名的 C# 2.0 动态事件订阅

c# - 扁平化枚举中的 Linq 组数据

java - 返回 Java 中的泛型类型

Swift:如何使这个函数通用

c# - 如何在不使用十字按钮的情况下关闭 Blazor 中的弹出窗口?

javascript - 使用 JavaScript 或 MVC Razor 返回页面