0

使用 VS 2017 - 获得以下内容:

InvalidCastException:无法将“System.Collections.Generic.List 1[<>f__AnonymousType143[System.String,System.String,System.String]]”类型的对象转换为“System.Collections.Generic.List`1[PharmaModel.Models.NewBrandForecast” ]'。

控制器:@foreach (var newBrandForecast in (List)ViewBag.newBrandForecasts)

产生于:

var getNewBrandForecastListing = (from nbf in Db.NewBrandForecasts
                                          group nbf by new
                                          {
                                            nbf.Brand,
                                            nbf.PricingScenario,
                                            nbf.Version
                                          } 
                                          into newGrp
                                          orderby newGrp.Key.Brand,
                                                  newGrp.Key.PricingScenario,
                                                  newGrp.Key.Version
                                          select new
                                          {
                                            newGrp.Key.Brand,
                                            newGrp.Key.PricingScenario,
                                            newGrp.Key.Version
                                          }).ToList();

        ViewBag.newBrandForecasts = getNewBrandForecastListing;

在没有分组的情况下进行查询不会出错,但使用上述内容时会出错。我是 Core MVC 的新手。任何帮助将不胜感激,但请尽可能保持解决方案的非技术性。

4

0 回答 0