开发者

making Union for three different entities that implements the same interface

开发者 https://www.devze.com 2023-03-10 06:33 出处:网络
I have 3 entities: RatedPrice, DailyPri开发者_开发百科ce and UtilizePrice. Each entity has Code member.

I have 3 entities: RatedPrice, DailyPri开发者_开发百科ce and UtilizePrice.

Each entity has Code member.

I would like to write a lambda query that returns IQueryable of the three sorted by Code member. I know I need to use somewhere in Union but I don't know how. Moreover, all the 3 entities has diffrences but are implements IPrice.

How can I do the query?


You must use something like this:

var data = ctx.RatedPrices.Select(p => new { p.Code, p.Price })
              .Concat(ctx.DailyPrices.Select(p => new { p.Code, p.Price })
                         .Contact(ctx.UtilizePrices.Select(p => new { p.Code, p.Price }))
              .OrderBy(p => p.Code);

Entity framework and linq-to-entities don't know what is the interface. You must use projection to some not mapped type (anonymous in the example) and deal with the result. Because you don't use entity inheritance from some base Price (otherwise you would not need this) you cannot expect that EF will return you instances of RetedPrice, DailyPrice and UtilizePrice in single result set.


Something like this maybe

var list = listRatedPrice.Cast<IPrice>()
.Concat(listDailyPrice.Cast<IPrice>()
.Concat(listUtilizePrice.Cast<IPrice>))).Orderby(p => p.Code);
0

精彩评论

暂无评论...
验证码 换一张
取 消