简体   繁体   中英

Linq projection into DTO objects

TL;DR: The code should show my intent, the place where it goes wrong is in the output statement, where I'm trying to set the StatusItemDTOs, which should contain all the fields from the anonymous objects, except LocationName.


I'm trying to query my db and extract only the fields I need using projection. The intended result is a LocationDTO wrapping StatusItemDTOs. The hard part is mapping over multiple DTOs in one query.

Where it goes wrong is in the output statement where I'm trying to set the StatusItemDTOs, which I have no idea how to do out from the dictionary values. If you look at my DTO classes in bottom of this post, you can see that the StatusItemDTO contains all the fields of the anonymous objects, except LocationName. The only reason I'm making the anonymous objects is because I don't know how to "store" the LocationName if I'm just selecting new StatusItemDTOs.

I feel pretty confident the query can be done shorter, and smarter, but I'm inexperienced with projection into DTOs and hope you can help.

var query = (from liq in Context.LocationItemQuantities
           where liq.Location.DepotId == depotId
           select new
           {
             LocationName = liq.Location.Name,
             ItemTypeName = liq.ItemType.Name,
             DepotQuantity = liq.Quantity,
             StandardQuantity = liq.StandardQuantity,
             MinimumQuantity = liq.MinQuantity,
           }).ToList();

        var output = from anon in query
                     group anon by anon.LocationName into g
                     select new LocationDTO
                     {
                         LocationName = g.Key,
                         StatusItemDTOs = g
                     };

My DTOs:

public class StatusItemDTO
{
    public int DepotQuantity { get; set; }
    public string ItemTypeName { get; set; }
    public DateTime ExpirationDate { get; set; }

    public int StandardQuantity { get; set; }
    public int? MinimumQuantity { get; set; }
}

public class LocationDTO
{
    public List<StatusItemDTO> StatusItemDTOs { get; set; }
    public string LocationName { get; set; }
}

Edit: Entity classes

 public class LocationItemQuantity : IEntity
    {
        public int Id { get; set; }

        public int Quantity { get; set; }

        public int? MinQuantity { get; set; }

        public int StandardQuantity { get; set; }

        public bool IsChecked { get; set; }

        public int LocationId { get; set; }

        public Location Location { get; set; }

        public int ItemTypeId { get; set; }

        public ItemType ItemType { get; set; }

    }

public class Location : IEntity
    {
        public int Id { get; set; }

        public List<LocationItemQuantity> LocationItemQuantities { get; set; }

        public string Name { get; set; }

        public int? DepotId { get; set; }

        public Depot Depot { get; set; }

    }

 public class ItemType : IEntity
{
    public int Id { get; set; }

    public string InternBarcode { get; set; }

    public string Description { get; set; }

    public string Name { get; set; }

    public List<LocationItemQuantity> LocationItemQuantities { get; set; }

    public List<ProductBatch> ProductBatches { get; set; }

    public List<Product> Products { get; set; }

}

You can project into the DTOs directly without using anonymous types like this:

var result =
    Context
    .LocationItemQuantities
    .Where(x=> x.Location.DepotId == depotId)
    .GroupBy(x => x.Location.Name)
    .Select(x => new LocationDTO
    { 
        LocationName = x.Key,
        StatusItemDTOs = x.Select(y => new StatusItemDTO
        {
            DepotQuantity = y.Quantity,
            StandardQuantity = y.StandardQuantity,
            MinimumQuantity = y.MinQuantity,
            ItemTypeName = y.ItemType.Name
        }).ToList()
    })
    .ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM