1

我正在尝试将包含一些集合的实体转换为Model但我不知道如何在模型中包含这些集合。

只是一个简单的例子,请看下面的模型:

Namespace Models

    Public Class ProductModel
        Public Property ID As Integer
        Public Property Description As String
        Public Property IsInStock as Boolean
        Public Property Orders As List(Of ProductOrderModel)

    End Class

    Public Class ProductOrderModel
        Public Property ID As Integer
        Public Property OrderDate As DateTime
        Public Property DeliveredDate As DateTime?
        Public Property ShippingAddress As String
    End Class

End Namespace

现在我可以轻松地转换为如下所示的产品模型:

Dim simplifiedProductModel as ProductModel =
    From p in dc.Products _
    Select New ProductModel With { _
        .ID = p.ProductID, _
        .Description = p.ProductDescription, _
        .IsInStock = p.ProductIsInStock, _
        .OrderCount = p.Orders.Count() _
        }

我想不通的是如何使用其简化模型来包含该集合。

Dim simplifiedProductModel as ProductModel =
    From p in dc.Products _
    Select New ProductModel With { _
    .ID = p.ProductID, _
    .Description = p.ProductDescription, _
    .IsInStock = p.ProductIsInStock, _
    .OrderCount = p.Orders.Count(), _
    .Orders = p.orders.???? ===> New ProductOrderModel 
        With { 
            .ID = ???? o.OrderID , 
            .OrderDate = ???? o.Createdate, 
            etc.  
            }
4

2 回答 2

1

像这样?

Dim simplifiedProductModel as ProductModel = From p in dc.Products _
                                        Select New ProductModel With { _
                                        .ID = p.ProductID, _
                                        .Description = p.ProductDescription, _
                                        .IsInStock = p.ProductIsInStock, _
                                        .OrderCount = p.Orders.Count(), _
                                        .Orders = p.orders.Select( _
                                            Function(o) New ProductOrderModel _
                                                With { _
                                                    .ID = o.OrderID , _
                                                    .OrderDate = o.Createdate, _
                                                    etc.  
                                                }
                                            )
                                         }
于 2013-03-01T16:02:23.783 回答
0

首先,您必须将您的声明修复Public Property Orders As String As List(Of ProductOrderModel)Public Property Orders As List(Of ProductOrderModel). 然后你可以使用这个

.Orders = (from order in p.orders
              Select New ProductOrderModel 
                     With { 
                             .ID = order .OrderID , 
                             .OrderDate = order.Createdate, 
                                  etc.
                          }).ToList() 
于 2013-03-01T16:04:35.393 回答