Here's the code that I'm attempting to do:
public IList<IOperator> GetAll()
{
using (var c = new MyDataContext())
{
return c.Operators.ToList();
}
}
Operator implements IOperator, but I'm getting the following compilation error:
Cannot implicitly convert type 'System.Collections.Generic.List<MyProject.Core.Operator>' to 'System.Collections.Generic.IList<MyProject.Core.Model.IOperator>'. An explicit conversion exists (are you missing a cast?)
How do I cast this to get what I need?
Try the Cast<>()
method:
return c.Operators.Cast<IOperator>().ToList();
If I change the code to the following:
public IList<IOperator> GetAll()
{
using (var c = new MyDataContext())
{
var operators = (from o in c.Operators
select o).Cast<IOperator>();
return operators.ToList();
}
}
it not only compiles but actually works! Thanks for the nudges in the right direction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With