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

Assuming InvoiceMaster derives from or implements InvoiceHD, and that you’re using C# 4 and .NET 4 or higher, you can just use generic variance:

return MstDtl.ToList<InvoiceHD>();

This uses the fact that an IEnumerable<InvoiceMaster> is an IEnumerable<InvoiceHD> because IEnumerable<T> is covariant in T.

Another way to solve it would be to change the declaration of MstDtl to use explicit typing:

IEnumerable<InvoiceMaster> MstDtl = ...;

(I’d also suggest following regular C# naming, where local variables start with a lower-case letter, but that’s a different matter.)

Leave a Comment

error code: 523