I am creating an explicit conversion operator to convert between a generic list of entity types to a generic list of model types. Does anyone know why I get the following e
Basically, you can't do this. In your operator either the input or output type must be of the type that declares the operator. One option might be an extension method, but to be honest LINQ Select comes pretty close by itself, as does List.
As an example of the extension method approach:
public static class MyListUtils {
public static List ToModel(this List entities)
{
return entities.ConvertAll(entity => (Model.objA)entity);
}
}
or more generally with LINQ Select:
public static class MyListUtils {
public static IEnumerable ToModel(
this IEnumerable entities)
{
return entities.Select(entity => (Model.objA)entity);
}
}
and just use someList.ToModel().