简体   繁体   English

自动映射器投影

[英]Automapper projection

i have question about Automapper projection. 我对Automapper投影有疑问。 I have entity: 我有实体:

public class SomeEntity
{
    public string Name { get; set; }

    public DateTime Date { get; set; }

    public string Price { get; set; }
}

And dto: 和dto:

public class SomeDto
{
    public string Name { get; set; }

    public int Month { get; set; }

    public string TotalPrice { get; set; }
}

How can I create next projection with automapper 如何使用自动映射器创建下一个投影

  var data = new List<SomeEntity>().AsQueryable()
        .GroupBy(p => new
        {
            p.Date.Month,
            p.Name
        }).Select(p => new
        {
            p.Key.Month,
            p.Key.Name,
            TotalPrice = p.Sum(entity => entity.Price)
        })
       .ToList()
       .Select(p => new SomeDto
        {
            Month = p.Month,
            Name = p.Name, 
            TotalPrice = p.TotalPrice
        });

You can't - AutoMapper LINQ projections won't really work with intermediate anonymous types that you've created. 您不能-AutoMapper LINQ投影不能真正与您创建的中间匿名类型一起使用。 In your first Select, you should remove the anonymous type: 在第一个“选择”中,应删除匿名类型:

var data = new List<SomeEntity>().AsQueryable()
    .GroupBy(p => new
    {
        p.Date.Month,
        p.Name
    }).Select(p => new SomeDto
    {
        Month = p.Key.Month,
        Name = p.Key.Name,
        TotalPrice = p.Sum(entity => entity.Price)
    })
   .ToList();

Then you don't need the second Select. 然后,您不需要第二个“选择”。 You're already manually projecting so AutoMapper won't help you there. 您已经在手动投影,因此AutoMapper无法在此帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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