dimanche 26 avril 2015

Use related entity multiple times in query

I'm trying to get a list of two tables into one list of a generic class I made.

My question is how I could get the fields from the related table without calling firstOrDefault for each field, please see sample code, I need a verly long line for fields Price and Cost, and I have a few more to do...

    Dim items As IQueryable(Of ItemMain)


    Return items.Where(Function(i) i.Status > -1).Select(Function(x) New ItemViewBasic() With {.ItemID = x.ItemID,
                                                                      .Name = x.Name,
                                                                      .BarcodeNumber = x.BarcodeNumber,
                                                                      .Price = x.ItemStores.Where(Function(itemStore) itemStore.StoreNo = GlobalValues.StoreID).FirstOrDefault().Price,
                                                                      .Cost = x.ItemStores.Where(Function(itemStore) itemStore.StoreNo = GlobalValues.StoreID).FirstOrDefault().Cost})

Aucun commentaire:

Enregistrer un commentaire