Cannot implicitly convert type System.Collections.Generic.List

Suprabhat Biswal

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.

Reza Aghaei

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot implicitly convert type to 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

From Dev

Cannot implicitly convert anonymous type to System.Collections.Generic.List

From Dev

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

From Dev

Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

From Dev

Cannot implicitly convert type 'System.Collections.IList' to 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<string>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<MODEL#1>' to 'System.Collections.Generic.List<Model#2>

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<string>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List<Model.Room

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<>' to 'System.Threading.Tasks.Task<>>

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List'

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List

From Dev

Web API Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type:

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<String>' to 'System.Collections.Generic.IEnumerable<turon.Model.Products_Products>

From Dev

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable

From Dev

Cannot Implicitly Convert Type 'System.Collections.Generic.List<>' to 'IList<>'. An explicit conversion exists (are you missing a cast?)

From Dev

Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<string>'

From Dev

Cannot implicitly convert type System.Collections.Generic.List<IEnumerable> to <IEnumerable

From Dev

Cannot implicitly convert type 'int' to 'System.Collections.Generic.List<QuickTest.Stock>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1> in C#

From Dev

Cannot implicitly convert type 'int?' to 'System.Collections.Generic.List<Model>

From Dev

C# Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Collections.Generic.ICollection

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.SqlClient.SqlParameter>' to 'System.Data.SqlClient.SqlParameter'

Related Related

  1. 1

    Cannot implicitly convert type to 'System.Collections.Generic.List

  2. 2

    Cannot implicitly convert type 'System.Collections.Generic.List

  3. 3

    Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

  4. 4

    Cannot implicitly convert anonymous type to System.Collections.Generic.List

  5. 5

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

  6. 6

    Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>

  7. 7

    Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

  8. 8

    Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

  9. 9

    Cannot implicitly convert type 'System.Collections.IList' to 'System.Collections.Generic.List

  10. 10

    Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<string>'

  11. 11

    Cannot implicitly convert type 'System.Collections.Generic.List<MODEL#1>' to 'System.Collections.Generic.List<Model#2>

  12. 12

    Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<string>'

  13. 13

    Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List

  14. 14

    Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List<Model.Room

  15. 15

    Cannot implicitly convert type 'System.Collections.Generic.List<>' to 'System.Threading.Tasks.Task<>>

  16. 16

    Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List'

  17. 17

    Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List

  18. 18

    Web API Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type:

  19. 19

    Cannot implicitly convert type 'System.Collections.Generic.List<String>' to 'System.Collections.Generic.IEnumerable<turon.Model.Products_Products>

  20. 20

    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable

  21. 21

    Cannot Implicitly Convert Type 'System.Collections.Generic.List<>' to 'IList<>'. An explicit conversion exists (are you missing a cast?)

  22. 22

    Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<string>'

  23. 23

    Cannot implicitly convert type System.Collections.Generic.List<IEnumerable> to <IEnumerable

  24. 24

    Cannot implicitly convert type 'int' to 'System.Collections.Generic.List<QuickTest.Stock>'

  25. 25

    Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1> in C#

  26. 26

    Cannot implicitly convert type 'int?' to 'System.Collections.Generic.List<Model>

  27. 27

    C# Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>'

  28. 28

    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Collections.Generic.ICollection

  29. 29

    Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.SqlClient.SqlParameter>' to 'System.Data.SqlClient.SqlParameter'

HotTag

Archive