0

I am using EF 5 and have a new property that I've defined in a partial class to extend the base database fields. It requires summing data from a related table.

[Display(Name = "Qty Allocated")]
        public decimal QtyAllocated
        {
            get { return this.AllocatedContainers == null ? 1 : this.AllocatedContainers.Sum(a => a.AllocatedQty); }
            //get { return 2;}
        }

This property returns the correct value....BUT, if I then use the following method to convert this to a view model, the returned value is 0. Note the view model inherits from the class:

public class InventoryContainerDetailListViewModel : InventoryContainerDetail

Method:

    public IEnumerable<InventoryContainerDetailListViewModel> ConvertClassToViewModel(IEnumerable<InventoryContainerDetail> entityList)
{
   IEnumerable<InventoryContainerDetailListViewModel> itemGrid =
        from l in entityList.ToList()
        select new InventoryContainerDetailListViewModel()
        {
            Id = l.Id,
            InventoryContainerHeaderId = l.InventoryContainerHeaderId,
            PONbr = l.ReceiptDetail == null ? (int?)null : l.ReceiptDetail.PODetail.POHeaderId,
            ReceiptDetailId = l.ReceiptDetailId,
            ItemId = l.ItemId,
            ItemDescription = l.Item.ShortDescription,
            QtyInContainer = l.QtyInContainer,
            //QtyAllocated = l.AllocatedContainers == null ? 0 : l.AllocatedContainers.Sum(a => a.AllocatedQty),
            Location = l.InventoryContainerHeader.Location.DisplayLocation
        };

    return itemGrid;
}

In this method, the input parameter entityList does show each item with the correct calculated values, but after the conversion, the value is always 0.

I assume this has something to do with the fact that I am inheriting from the base class, but can someone shed some light on this?

4

1 回答 1

0

我不认为原因是继承。更可能的原因是AllocatedContainers空集合(在创建视图模型实例时没有分配它)。

于 2013-03-06T11:30:20.213 回答