0

我有以下模型和视图模型(为简洁而编辑):

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

如何内部连接两个表并映射到视图模型?

    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;

此代码不起作用。

4

1 回答 1

1

您需要首先通过以下方式实现结果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);
于 2012-08-16T15:58:06.057 回答