Mapping LinqToEntities join to ViewModel

大城市里の小女人 提交于 2019-12-25 05:18:12

问题


I have the following Models and ViewModels (edited for brevity):

     Order Model:
            OrderId
            ShippingAddressId 
            .....
    Address Model:
            AddressId
            .....
    OrderViewModel:
            Some Property from Order Model and Address Model

how can inner join two tables and map to a viewmodel ?

    var query= from o in ctx.Orders
        join addr in ctx.Addresses
        on o.ShippingAddressId equals addr.AddressId
        select new OrderViewModel.InjectFrom(o)
                              .InjectFrom(addr)
                              as OrderViewModel;

this code doesn't work .


回答1:


You need to first materialize the result with ToList:

var query = (from o in ctx.Orders
             join addr in ctx.Addresses
             on o.ShippingAddressId equals addr.AddressId
             select new { o, addr }
            ).ToList()
             .Select(x => new OrderViewModel().InjectFrom(x.o)
                                              .InjectFrom(x.addr)
                          as OrderViewModel);


来源:https://stackoverflow.com/questions/11988573/mapping-linqtoentities-join-to-viewmodel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!