The essence of the case makes the case on the discriminator

Is there a way to throw a query inside a subclass?

If I requested only one subclass, I would do

auctions.OfType<AuctionBid>().Where(auctionBid => auctionBid.AuctionBidProp)

Now I would like to make a case by type of subclass

auctions.Where(auction => 
    (auction is AuctionBid) && ((AuctionBid) auction).Prop == 1 
    || (auction is AuctionBuy) && ((AuctionBuy) auction).Prop == 1)

Is there any way to do this?

Of course, the above line gives an error: LINQ to Entities only supports listing EDM primitives or enumeration types.

+4
source share
1 answer

The only option I see is to request additional types and combine the results:

auctions.OfType<AuctionBid>()
        .Where(auctionBid => auctionBid.AuctionBidProp == 1)
        .Cast<AuctionBase>
        .Concat(auctions.OfType<AuctionBuy>()
                        .Where(auctionBuy => auctionBuy.AuctionBidProp == 1)
               )
        .OrderBy(ab => ab.Prop1)
        .Skip(pages * pageSize)
        .Take(pageSize)

This will trigger a request UNION ALL. Not the most efficient approach compared to manual SQL, but perhaps acceptable (you'll have to try).

+3
source

Source: https://habr.com/ru/post/1545414/


All Articles