简体   繁体   English

LINQ EF分组和包含例外

[英]LINQ EF Grouping and Include exception

Need help in creating LINQ query to group and filter with related entities. 在创建LINQ查询以对相关实体进行分组和过滤时需要帮助。

Here is my model classes. 这是我的模型课。

public class Application
    {
        [DisplayName("Application")]
        public int ApplicationId { get; set; }
        [DisplayName("Application")]
        public string Name { get; set; }
        public List<DashboardEntry> DashboardEntries { get; set; }
    }

public class Cluster
    {
        [DisplayName("Cluster")]
        public int ClusterId { get; set; }
        [DisplayName("Cluster")]
        public string Name { get; set; }
    }

[Bind(Exclude = "AlbumId")]
    public class DashboardEntry
    {
        [ScaffoldColumn(false)]
        public int DashboardEntryId { get; set; }        
        public int ClusterId { get; set; }        
        public int ApplicationId { get; set; }        
        public HealthStatusIndicator Status { get; set; }
        public string Incident { get; set; }
        public string Remarks { get; set; }

        public virtual Cluster Cluster { get; set; }
        public virtual Application  Application { get; set; }
    }

Index action method is as follows 索引动作方法如下

public ActionResult Index()
        {
            //var dashboardEntries = db.DashboardEntries.Include(d => d.Application).Include(d => d.Cluster);

            var dashboardEntries = db.DashboardEntries
                                   .Include(d => d.Application)
                                   .Include(d => d.Cluster)                                   
                                   .GroupBy(d => d.Application);

            return View(dashboardEntries.ToList());
        }

In the view, model declaration is as below. 在视图中,模型声明如下。

@model IEnumerable<HealthCheckIndex.Models.DashboardEntry>

I'm getting an error 我遇到错误

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery1[System.Linq.IGrouping2[HealthCheckIndex.Models.Application,HealthCheckIndex.Models.DashboardEntry]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[HealthCheckIndex.Models.DashboardEntry]'. 传递到字典中的模型项的类型为“ System.Data.Entity.Infrastructure.DbQuery1 [System.Linq.IGrouping2 [HealthCheckIndex.Models.Application,HealthCheckIndex.Models.DashboardEntry]]”,但是此字典需要的模型项为类型“ System.Collections.Generic.IEnumerable`1 [HealthCheckIndex.Models.DashboardEntry]”。

If I change the model declaration in view as below, I'm getting a another error that Cluster is not accessible. 如果按如下所示更改视图中的模型声明,则会收到另一个错误,提示无法访问Cluster

@model IEnumerable> @model IEnumerable>

I want to group the dashboard entries into different applications and filter the groups by choosing the max dashboard entry from each group. 我想将仪表板条目分为不同的应用程序,并通过从每个组中选择最大仪表板条目来过滤组。

The type that you are passing currently to the view does not match the specified type 您当前传递给视图的类型与指定的类型不匹配

@model IEnumerable<HealthCheckIndex.Models.DashboardEntry>

Currently you are passing something like a dictionary where the key is Application and the value is a IEnumerable of HealthCheckIndex.Models.DashboardEntry 当前,您正在传递类似字典的内容,其中的键是Application,值是HealthCheckIndex.Models.DashboardEntry的IEnumerable。

In order to make it you have one of 2 options: 为了使它具有两个选项之一:

  1. Replace the last line of the controller action with 将控制器操作的最后一行替换为

    return View(dashboardEntries.SelectMany(c=> c).ToList());

  2. Change model definition in the view to match the list your returning 在视图中更改模型定义以匹配返回的列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM