Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法隐式转换类型System.Collections.Generic.List_C#_Asp.net_Linq_List - Fatal编程技术网

C# 无法隐式转换类型System.Collections.Generic.List

C# 无法隐式转换类型System.Collections.Generic.List,c#,asp.net,linq,list,C#,Asp.net,Linq,List,异常:无法将类型“System.Collections.Generic.List”隐式转换为 'System.Collections.Generic.List' 我不知道我犯了什么错误。这就是我所遵循的结构:- namespace XMailerData.DAL { public class Data : IData, IDisposable { XmailerEntities context = new XmailerEntities(); pu

异常:无法将类型“System.Collections.Generic.List”隐式转换为 'System.Collections.Generic.List'

我不知道我犯了什么错误。这就是我所遵循的结构:-

namespace XMailerData.DAL
{
    public class Data : IData, IDisposable
    {
        XmailerEntities context = new XmailerEntities();
        public List<PageContentModel> GetPageContent()
        {
            List<PageContentModel> lPageContent = (from pc in context.T_PageContent
                                                   select new PageContentModel
                                                   {
                                                       contentId = pc.ContentId,
                                                       description = pc.Description
                                                   }).AsEnumerable().ToList();
            return lPageContent;
        }
    }
}
现在,在另一个名为Classes的类库中,我试图调用下面提到的函数

namespace Classes.Helper
{
    public class DAL
    {
        public static List<PageContentModel> FetchPageContent()
        {
            IData iData;
            List<PageContentModel> lPageContent = new List<PageContentModel>();
            lPageContent = iData.GetPageContent(); // Here i'm getting an error 
            return lPageContent;
        }
    }
}
在所有尝试中,我无法解决以下错误:-

异常:无法隐式转换类型 'System.Collections.Generic.List' 到 'System.Collections.Generic.List'

有人能帮我克服这个问题,承认我犯了什么错误,因为我犯了这样的错误吗

结论:

谢谢你的洞察力,这真的很有帮助。现在我已经修改了我的代码,它现在可以正常工作了。这是我现在所做的

namespace Classes.Helper
{
    public class DAL
    {
        public static List<PageContent> FetchPageContent()
        {
            IData iData = new Data();
            List<PageContent> lPageContent = new List<PageContent>();
            lPageContent = iData.GetPageContent()
                .Select(pc => new PageContent
                {
                    pageId = pc.pageId,
                    contentId = pc.contentId,
                    description = pc.description,
                }).ToList();
            return lPageContent;
        }
    }
}
名称空间类.Helper
{
公共类DAL
{
公共静态列表FetchPageContent()
{
IData IData=新数据();
List lPageContent=新列表();
lPageContent=iData.GetPageContent()
.选择(pc=>新建页面内容
{
pageId=pc.pageId,
contentId=pc.contentId,
description=pc.description,
}).ToList();
返回内容;
}
}
}

为了避免歧义,我将属性类从PageContentModel重命名为PageContent。感谢Reza Aghaei、Monroe Thomas和Nikita分享建议。Reza Aghaei和Monroe Thomas都可以接受,但不可能接受两个答案,所以我只接受一个答案,其余的则投赞成票

错误是自描述的:

Cannot implicitly convert type 
'System.Collections.Generic.List<XMailerData.CompositeModel.PageContentModel>'
 to 'System.Collections.Generic.List<Classes.CompositeModel.PageContentModel>'

错误是自描述的:

Cannot implicitly convert type 
'System.Collections.Generic.List<XMailerData.CompositeModel.PageContentModel>'
 to 'System.Collections.Generic.List<Classes.CompositeModel.PageContentModel>'

如果无法更改代码以共享相同的PageContentModel类类型,还可以尝试以下操作,以便在属性兼容的情况下将一种类型的实例转换为另一种类型:

lPageContent = iData.GetPageContent()
    .Select(content => new Classes.CompositeModel.PageContentModel
          {  
              pageId = content.pageId,
              contentId = content.contentId,
              description = content.description
          })
    .ToList();

如果无法更改代码以共享相同的PageContentModel类类型,还可以尝试以下操作,以便在属性兼容的情况下将一种类型的实例转换为另一种类型:

lPageContent = iData.GetPageContent()
    .Select(content => new Classes.CompositeModel.PageContentModel
          {  
              pageId = content.pageId,
              contentId = content.contentId,
              description = content.description
          })
    .ToList();

XMailerData.CompositeModel.PageContentModel
Classes.CompositeModel.PageContentModel
具有相同的属性,但它们位于不同的命名空间中,这使它们成为两个不同的类。
因此,类
Data
应该引用
Classes.CompositeModel.PageContentModel
而不是
XMailerData.CompositeModel.PageContentModel

XMailerData.CompositeModel.PageContentModel
类。CompositeModel.PageContentModel
具有相同的属性,但它们驻留在不同的命名空间中,这使他们成为两个不同的阶层。
因此,类
Data
应该引用
Classes.CompositeModel.PageContentModel
而不是
XMailerData.CompositeModel.PageContentModel
XMailerData.CompositeModel.PageContentModel

尽管
XMailerData.CompositeModel.PageContentModel
Classes.CompositeModel.PageContentModel
共享完全相同的属性,它们是两种完全不同的类型。所以您应该基于另一个类型转换(或构建)类型。好的,当我从office返回时,我将检查它们是否在多个程序集中编译@Alizera我尝试转换它,但没有成功。虽然
XMailerData.CompositeModel.PageContentModel
类.CompositeModel.PageContentModel
共享完全相同的属性,但它们是两种完全不同的类型。所以您应该基于另一个类型转换(或构建)类型。好的,当我从office返回时,我将检查它们是否在多个程序集中编译@Alizera我尝试转换它,但没有成功。谢谢建议,我下班回家后会尝试。谢谢建议,我下班回家后会尝试。谢谢建议,我下班回家后会尝试。谢谢建议,我下班回家后会尝试。谢谢建议,我下班回家后会尝试。
lPageContent = iData.GetPageContent()
    .Select(content => new Classes.CompositeModel.PageContentModel
          {  
              pageId = content.pageId,
              contentId = content.contentId,
              description = content.description
          })
    .ToList();