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

标签 c# linq

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

Cannot implicitly convert type System.Collections.Generic.IEnumerable<x> to System.Collections.Generic.List<x>. An explicit conversion exists (are you missing a cast?)

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);

最佳答案

这里的问题是这样的:

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();

关于c# - 无法将类型 System.Collections.Generic.IEnumerable 隐式转换为 System.Collections.Generic.List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37857207/

相关文章:

c# - 如何在 RibbonButton 中设置 Horizo​​ntalContentAlignment

jquery - 从 Angular 对象数组中选择特定属性

c# - SQL 代码比 C# 代码快吗?

c# - 如何从UI将项目添加到ComboBox?

c# - ASP.NET 站点在无法将 DBNull.Value 转换为类型 'System.Guid' 时崩溃

c# - 如何使用 Lambda 或 Linq 语句执行以下操作?

c# - 将 Field<> 与 Linq to SQL 结合使用

c# - 将返回表达式的方法引用到另一个方法的语法?

c# - 将动态对象转换为 NameValueCollection

c# - 为相同的小写和大写字符串创建不同的 GUID