【问题标题】:LINQ - Sum depending on multiple conditionsLINQ - 根据多个条件求和
【发布时间】:2020-02-05 21:04:15
【问题描述】:

我想创建一个 linq 查询来汇总客户订单的总数。根据客户已经订购的产品,我需要根据产品类型应用不同的倍数。例如,客户订购了一种 SaaS 和两种 PaaS 产品。对于按该顺序排列的每个 PaaS 产品,我想将价格乘以 1.5,而对于 SaaS,价格将乘以 0.8。

现在我有一个代码,它会给我总金额,不管产品是什么产品。如何编写查询,根据客户订购的产品类型汇总每个客户的产品?我还希望它采用字典的形式,因此对于每个 ID,我将拥有所有订单的总和。

这是我准备的一些代码,以便更好地理解:

    public class Calculator
    {
        public void Calculate()
        {           
            // Let's pretend that customers comes from some sort of DB context
            IDictionary<int, decimal> totalls = customers
                .ToDictionary(c => c.ID, c => c.OrderItems.Sum(oi => oi.PriceNet));

            foreach (KeyValuePair<int, decimal> item in totalls)
            {
                Console.WriteLine($"{item.Key} {item.Value}");
            }
        }
    }

    public class Order
    {
        public Customer Customer { get; set; }
        public ICollection<OrderItem> OrderItems { get; set; }

        public Order()
        {
            this.OrderItems = new List<OrderItem>();
        }
    }

    public class OrderItem
    {
        public Customer Customer { get; set; }
        public Order Order { get; set; }
        public decimal PriceNet { get; set; }
        public Product Product { get; set; }
    }

    public class Customer
    {
        public int ID { get; set; }
        public ICollection<Order> Orders { get; set; }
        public ICollection<OrderItem> OrderItems { get; set; }

        public Customer()
        {
            this.Orders = new List<Order>();
            this.OrderItems = new List<OrderItem>();
        }
    }

    public class Product
    {
        public EnumProductType Type { get; set; }
    }

    public enum EnumProductType
    {
        SAAS,
        PAAS
    }

【问题讨论】:

  • 您需要使用AsEnumerable 切换到LINQ to Objects 并编写一个方法来计算OrderItems 的正确价格。

标签: .net entity-framework linq


【解决方案1】:

请试试这个:

var totalls = customers.ToDictionary(customer => customer.ID, customer => customer.OrderItems.Sum(oi => oi.Product.Type == EnumProductType.PAAS
                ? oi.PriceNet * 1.5m
                : oi.PriceNet * 0.8m));

所以,请修改您的Calculator::Calculate 方法如下:

public class Calculator
{
    public void Calculate()
    {           
        // Let's pretend that customers comes from some sort of DB context
        /*IDictionary<int, decimal> totalls = customers
            .ToDictionary(c => c.ID, c => c.OrderItems.Sum(oi => oi.PriceNet));*/

        var totalls = customers.ToDictionary(customer => customer.ID, customer => customer.OrderItems.Sum(oi => oi.Product.Type == EnumProductType.PAAS
                ? oi.PriceNet * 1.5m
                : oi.PriceNet * 0.8m));

        foreach (KeyValuePair<int, decimal> item in totalls)
        {
            Console.WriteLine($"{item.Key} {item.Value}");
        }
    }
}

【讨论】:

    猜你喜欢
    • 2020-03-05
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    • 2021-01-25
    • 2022-01-12
    • 1970-01-01
    • 2021-05-05
    相关资源
    最近更新 更多