简体   繁体   中英

Cannot implicitly convert type System.Collections.Generic.List

Exception: Cannot implicitly convert type 'System.Collections.Generic.List<ModelClass>' to 'System.Collections.Generic.List<ModelClass>'

I can't figure out what mistake I have made. This is the structure what I am following:-

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

In above class I am trying to return a List that contain result generated from linq expression and PageContentModel is defined in same namespace but with different class name.

namespace XMailerData.CompositeModel
{
    public class PageContentModel
    {
        public int pageId { get; set; }
        public int contentId { get; set; }
        public string description { get; set; }
    }
}

Now, in my another class library named Classes I trying to call out that function as mentioned below

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

Here I am trying to bind the result from iData.GetPageContent() to another list that is defined in another class but I am failing to do so.

namespace Classes.CompositeModel
{
    public class PageContentModel
    {
        public int pageId { get; set; }
        public int contentId { get; set; }
        public string description { get; set; }
    }
}

Of all my tries I am not able to resolve following error:-

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

Can someone help me to overcome this problem and acknowledge me what mistake I have made because of which I got such type of error.

Conclusion:

Thanks for the insight it was really helpful. Now I have modified my code, it's now working properly. Here's what i have done now.

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

To avoid ambiguity I have renamed the property class from PageContentModel to PageContent . Thanks Reza Aghaei , Monroe Thomas and Nikita for sharing suggestion. Both Reza Aghaei , Monroe Thomas are acceptable but it won't be possible to accept two answer so I'm accepting one answer and upvoting rest.

XMailerData.CompositeModel.PageContentModel and Classes.CompositeModel.PageContentModel has same properties,but they resides in different namespaces, that makes them two different classes. So, the class Data should refer Classes.CompositeModel.PageContentModel and not XMailerData.CompositeModel.PageContentModel .

If you can't change the code to share the same PageContentModel class type, you can also try something like the following to convert an instance of one type to another if the properties are compatible:

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

The error is self describing:

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

Take a look at Namespaces.

Even though these two class have the same name and same properties, while they are in different namespaces, they can't be converted to each other implicitly.

lPageContent = iData.GetPageContent(); // Here i'm getting an error

Here you are putting result of your GetPageContent method which its type is List<XMailerData.CompositeModel.PageContentModel> to lPageContent which its type is List<Classes.CompositeModel.PageContentModel>

You can fix the problem this way (which also Monroe Thomas mentioned in his good answer)

namespace Classes.Helper
{
    public class DAL
    {
        public static List<Classes.CompositeModel.PageContentModel> FetchPageContent()
        {
            IData iData= new Data();
            lPageContent = iData.GetPageContent()
                                .Select(x=> 
                                new Classes.CompositeModel.PageContentModel()
                                {  
                                    pageId = x.pageId,
                                    contentId = x.contentId,
                                    description = x.description
                                }).ToList();
            return lPageContent;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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