实体框架在鉴别器上做案例 [英] Entity framework do case on discriminator
本文介绍了实体框架在鉴别器上做案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我只是查询一个子类,我会做
auction.OfType< AuctionBid>()。其中(auctionBid => auctionBid.AuctionBidProp)
pre>
现在我想做一个类的子类
auction.Where(auction =>
(拍卖是AuctionBid)&((AuctionBid)拍卖).Prop == 1
||(拍卖AuctionBuy)&((AuctionBuy)拍卖).Prop == 1)
有没有以上方法可以做到吗?
当然上面提到的错误:LINQ to Entities只支持投射EDM原语或枚举类型。
解决方案我看到的唯一选项是单独查询子类型并连接结果:
auction.OfType< AuctionBid>()
.Where(拍卖Bid => auctionBid.AuctionBidProp == 1)
.Cast< AuctionBase>
.Concat(auction.OfType< AuctionBuy>()
.Where(auctionBuy => auctionBuy.AuctionBidProp == 1)
)
.OrderBy(ab => ab .Prop1)
.Skip(pages * pageSize)
.Take(pageSize)
这将生成一个
UNION ALL
查询。与手工制作的SQL相比,并不是最有效的方法,但可以接受(您必须尝试)。Is there a way to cast inside query to subclass?
If I would be quering only single subclass I would do
auctions.OfType<AuctionBid>().Where(auctionBid => auctionBid.AuctionBidProp)
Now I would like to do a case on type of subclass
auctions.Where(auction => (auction is AuctionBid) && ((AuctionBid) auction).Prop == 1 || (auction is AuctionBuy) && ((AuctionBuy) auction).Prop == 1)
Is there the way to do it?
Of course line above gives an error: LINQ to Entities only supports casting EDM primitive or enumeration types.
解决方案The only option I see is to query the sub types separately and concatenate 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 generate a
UNION ALL
query. Not the most efficient approach when compared to hand-crafted SQL, but maybe acceptable (you'll have to try).这篇关于实体框架在鉴别器上做案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文