.net - Inheritance and Polymorphism in LINQ Lambdas -
i've got couple of classes inheriting base class
public mustinherit class baseclass public property name string end class public mustinherit class classa inherits baseclass public property string end class public mustinherit class classb inherits baseclass public property somethingelse string end class
i want able use single expression query multiple lists of objects, of inherit same base class
public function dostuff(expression system.linq.expressions.expression(of func(of baseclass, boolean))) dim lista new list(of classa) ''populating elsewhere dim listb new list(of classb) ''and dim resultseta = lista.where(expression) ''problems on line dim resultsetb = lista.where(expression) ''and end function
since both classa
, classb
inherit same base class, , since linq query against base class, should work. expression
can refer properties of base class guaranteed present on both derived classes following compile-time typing error:
value of type 'system.linq.expressions.expression(of system.func(of baseclass, boolean))' cannot converted 'system.linq.expressions.expression(of system.func(of classa, boolean))'
is there way can add widening conversion (or similar) allow work?
or need provide exact same query multiple times against each derived class?
you need asqueryable()
list (c# syntax below)
var resulta = lista.asqueryable().where(expression);
you can cast results original types if need
foreach (var x in resulta) { response.write(((classa)x).something); }
Comments
Post a Comment