RichC
RichC

Reputation: 7879

How to convert to a strong type that has a collection?

I'm trying to convert an entity that contains some collections to Model but I can't figure out how to include the collections in the model.

For just a simple example of this, please see the model below:

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

Now I can easily cast into the Product Model shown below:

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() _
        }

What I can't figure out is how to include the collection using its simplified model as well.

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.  
            }

Upvotes: 1

Views: 68

Answers (2)

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

First you must fix your Public Property Orders As String As List(Of ProductOrderModel) declaradtion as Public Property Orders As List(Of ProductOrderModel). Then you can use this

.Orders = (from order in p.orders
              Select New ProductOrderModel 
                     With { 
                             .ID = order .OrderID , 
                             .OrderDate = order.Createdate, 
                                  etc.
                          }).ToList() 

Upvotes: 0

D Stanley
D Stanley

Reputation: 152521

Like this?

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.  
                                                }
                                            )
                                         }

Upvotes: 1

Related Questions