无法将System.Collections.Generic.IEnumerable类型隐式转换为System.Collections.Generic.List

时间:2016-06-16 10:57:59

标签: c# linq

我需要为每个订单只获得一条记录,但如果有广告,我需要连接到记录。但是当我使用Concat时,我收到了这个错误。

  

无法将System.Collections.Generic.IEnumerable<x>类型隐式转换为System.Collections.Generic.List<x>。存在显式转换(您是否错过了演员?)

DateTime now = DateTime.Now;
var pom = (from moduleNews in db.CMS_News_NewsInWebModule
           join module in db.CMS_News_WebModule on moduleNews.moduleKey equals module.moduleKey
           join newsEdnm in db.CMS_News_NewsToEditionNumber on moduleNews.newsGUID equals newsEdnm.newsGUID
           where module.moduleKey == id && newsEdnm.dateToBePublished < now && newsEdnm.CMS_News_Edition_Number.editionID == 2
           //orderby newsEdnm.dateToBePublished descending, moduleNews.order
           select
           new NewsModules
           {
               order = moduleNews.order,
               id = moduleNews.newsInWebModuleId,
               moduleKey = module.moduleKey,
               klientName = module.klientName,
               maxNews = module.maxNews,
               newsGUID = moduleNews.newsGUID,
               title = moduleNews.CMS_News_News.title,
               isAdvertise = moduleNews.isAdvertise,
               advertise = moduleNews.advertise,
               dateToBePublished = newsEdnm.dateToBePublished.Value
           }).OrderBy(x => x.order).ThenByDescending(x => x.dateToBePublished).ToList(); //.GroupBy(x => x.order); 

pom = pom.GroupBy(n => n.order).Select(p => p.FirstOrDefault()).ToList();
var advirtise = (from moduleNews in db.CMS_News_NewsInWebModule
                 join module in db.CMS_News_WebModule on moduleNews.moduleKey equals module.moduleKey

                 where module.moduleKey == id && moduleNews.isAdvertise
                 //orderby newsEdnm.dateToBePublished descending, moduleNews.order
                 select
                 new NewsModules
                 {
                     order = moduleNews.order,
                     id = moduleNews.newsInWebModuleId,
                     moduleKey = module.moduleKey,
                     klientName = module.klientName,
                     maxNews = module.maxNews,
                     newsGUID = moduleNews.newsGUID,
                     title = moduleNews.CMS_News_News.title,
                     isAdvertise = moduleNews.isAdvertise,
                     advertise = moduleNews.advertise,
                     dateToBePublished = null
                 }).OrderBy(x => x.order).ToList();

pom = pom.Concat(advirtise);

1 个答案:

答案 0 :(得分:4)

问题在于:

pom = pom.Concat(advirtise);
 ^    ^-------+------------^
 |            |
 |            +-----> returns IEnumerable<Something> -----+
 |                                                        |
 +----- which you try to store into List<Something> <-----+

您需要创建另一个List<Something>,或将pom的类型更改为IEnumerable<Something>

pom = pom.Concat(advirtise).ToList();

这是一个演示问题的简短程序:

var pom = new[] { "a", "b", "c" }.ToList();
pom = pom.Concat(new[] { "x", "y", "z" }); // CS0266 - cannot implicitly ...

以下是应该如何写的:

var pom = new[] { "a", "b", "c" }.ToList();
pom = pom.Concat(new[] { "x", "y", "z" }).ToList();
相关问题