1

I am trying to return list. Any suggestion would be highly appreciable. Thanks in Advance.

It throws this error:

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

Code is:

public class GetFlowers
{
   public IList<testDomain> getFlowerList()
   {
      TestContainer ctx = new TestContainer();
      return ctx.tblFlowers.ToList();
   }
}

public class testDomain
{
    public string Name { get; set; }
    public int quantity { get; set; }
}
1
  • It Throws error as Error Cannot implicitly convert type 'System.Collections.Generic.List<HTentityFramework.tblFlower>' to 'System.Collections.Generic.List<HTentityFramework.testDomain>' Commented Mar 17, 2012 at 2:35

1 Answer 1

4

The error is pretty clear - just read it!

The type you're returning from getFlowerList is IList<testDomain> - yet you're selecting from an EF object set that's called tblFlowers.

The error clearly says that this is an IList<tblFlower> - a list of tblFlower objects.

Your code cannot just convert a list of tblFlowers to a list of testDomain - that's the whole point.

You need to either provide a conversion yourself, or you need to return a IList<tblFlower> from your method:

public IList<tblFlower> getFlowerList()   <==== return an IList<tblFlower> here!!
{
    TestContainer ctx = new TestContainer();
    return ctx.tblFlowers.ToList();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for solving the issue. But dont we use TestDomain classes in Ilist?
@Usham: if you select from tblFlowers, then you get back a list of tblFlower objects - so you need to return those ..... if you want to "hide" that fact and return some other type instead - it's up to you to provide some kind of a conversion/mapping from the real, actual type returned by EF (tblFlower) to some other datatype that you might have defined. Again: your job, and it needs to be clear, explicit - no magic in .NET can do this for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.