How to combine 2different IQueryable/List/Collection with same base class? LINQ Union and Covariance issues -
How to combine 2different IQueryable/List/Collection with same base class? LINQ Union and Covariance issues -
i trying combine (union or concat) 2 lists/collection one. 2 lists have mutual base of operations class. e.g. i've tried this:
iqueryable<contractitem> contractitems = myrepository.retrievecontractitems(); iqueryable<changeorderitem> changeorderitems = myrepository.retrievechangeorderitems(); iqueryable<itembase> folderitems = contractitems.concat<itembase>(changeorderitems);
but getting linq error dbunionallexpression requires arguments compatible collection resulttypes.
anybody know how properly? thing google stackoverflow question: linq union objects same base of operations class
thanks.
use cast
operator:
iqueryable<itembase> folderitems = contractitems.cast<itembase>().concat(changeorderitems.cast<itembase>());
the reply other question works linq objects, not linq entities or linq sql.
alternatively, can convert linq objects calling asenumerable
:
iqueryable<itembase> folderitems = contractitems.asenumerable().concat<itembase>(changeorderitems);
however, take care in linq objects; concat
work without overhead (iterating through both collections database), union
pull 1 of collections exclusively database , iterate through other.
linq union covariance
Comments
Post a Comment