首页 > 解决方案 > Linq Group Object

问题描述

I have a list of customer objects where I am having trouble understanding how to properly group this into a new object.

public class CustomerOrders
{
    public string CustomerId { get; set; }

    public string OrderType { get; set; }

    public DateTime OrderDate { get; set; }
    
    public string Account { get; set; }

    public decimal Amount { get; set; }

    public string ClientId { get; set; }
}   

I need to have a new object list to access with the CustomerOrders grouped by:

Also is there a way to add a few null properties where I can populate later through an iteration to this new list for example "Approved" to the Grouped fields?

标签: c#linq

解决方案


这是实现此目的的一种方法。如果这个类是一个实体,并且 account、amount 和 clientid 是另一个表中可以连接到客户数据的字段,那就更好了。

下面是我来自 linqpad 的示例。

void Main()
{
    List<CustomerOrders> orders = new List<CustomerOrders>() {
        new CustomerOrders() {CustomerId = "1", OrderType = "1", OrderDate = DateTime.Now, Account = "1234", Amount = 2.2M, ClientId = "asdf"},
        new CustomerOrders() {CustomerId = "1", OrderType = "1", OrderDate = DateTime.Now, Account = "1235", Amount = 5.3M, ClientId = "hjkl"},
        new CustomerOrders() {CustomerId = "2", OrderType = "3", OrderDate = DateTime.Now, Account = "1236", Amount = 6.2M, ClientId = "zxcv"}
    };
    
    var data = 
        from o in orders
        group new { o.CustomerId, o.OrderType, o.OrderDate } by new { o.CustomerId, o.OrderType, o.OrderDate } into grouped
        select new Results {
            CustomerId = grouped.First().CustomerId,
            OrderType = grouped.First().OrderType,
            OrderDate = grouped.First().OrderDate,
            Data = 
                orders.Where(x => 
                    x.CustomerId == grouped.First().CustomerId &&
                    x.OrderType == grouped.First().OrderType &&
                    x.OrderDate == grouped.First().OrderDate
                ).Select(d => new CustomerOrderData {
                    Account = d.Account,
                    Amount = d.Amount,
                    ClientId = d.ClientId
                })
        };
        
    data.Dump();
}

    public class CustomerOrders
    {
        public string CustomerId { get; set; }
    
        public string OrderType { get; set; }
    
        public DateTime OrderDate { get; set; }
        
        public string Account { get; set; }
    
        public decimal Amount { get; set; }
    
        public string ClientId { get; set; }
    }
    
    public class CustomerOrderData {
        public string Account { get; set; }
    
        public decimal Amount { get; set; }
    
        public string ClientId { get; set; }
    }
    
    public class Results
    {
        public string CustomerId { get; set; }
    
        public string OrderType { get; set; }
    
        public DateTime OrderDate { get; set; }
        
        public bool? Approved { get; set; } // adding null property
        
        public IEnumerable<CustomerOrderData> Data { get; set; }
    }

推荐阅读