C# Linq群对象
我有一个客户对象列表,我很难理解如何正确地将其分组到一个新对象中C# Linq群对象,c#,linq,C#,Linq,我有一个客户对象列表,我很难理解如何正确地将其分组到一个新对象中 public class CustomerOrders { public string CustomerId { get; set; } public string OrderType { get; set; } public DateTime OrderDate { get; set; } public string Account { get; set; } public
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; }
}
我需要有一个新的对象列表,以便使用按以下分组的CustomerOrder进行访问:
- 客户ID
- 订单类型
- 订单日期
- 帐户、金额和客户ID的列表,这些重复用于CustomerId、OrderType和OrderDate
还有一种方法可以添加一些空属性,我可以在以后通过迭代填充到这个新列表中,例如在分组字段中添加“Approved”?这里有一种方法可以实现这一点。如果这个类是一个实体,account、amount和clientid是另一个表中的字段,可以连接到customer数据,那就更好了 下面是我来自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; }
}
void Main()
{
列表顺序=新列表(){
新客户订单(){CustomerId=“1”,OrderType=“1”,OrderDate=DateTime。现在,Account=“1234”,Amount=2.2M,ClientId=“asdf”},
新客户订单(){CustomerId=“1”,OrderType=“1”,OrderDate=DateTime。现在,Account=“1235”,Amount=5.3M,ClientId=“hjkl”},
新客户订单(){CustomerId=“2”,OrderType=“3”,OrderDate=DateTime。现在,Account=“1236”,Amount=6.2M,ClientId=“zxcv”}
};
风险值数据=
按顺序从o开始
通过新建{o.CustomerId,o.OrderType,o.OrderDate}将新的{o.CustomerId,o.OrderType,o.OrderDate}分组为分组
选择新结果{
CustomerId=grouped.First().CustomerId,
OrderType=grouped.First().OrderType,
OrderDate=grouped.First().OrderDate,
数据=
订单。其中(x=>
x、 CustomerId==分组的.First().CustomerId&&
x、 OrderType==分组的.First().OrderType&&
x、 OrderDate==分组的.First().OrderDate
).选择(d=>new CustomerOrderData{
帐户=d.帐户,
金额=d.金额,
ClientId=d.ClientId
})
};
data.Dump();
}
公共类客户订单
{
公共字符串CustomerId{get;set;}
公共字符串OrderType{get;set;}
public DateTime OrderDate{get;set;}
公共字符串帐户{get;set;}
公共十进制数{get;set;}
公共字符串ClientId{get;set;}
}
公共类CustomerOrderData{
公共字符串帐户{get;set;}
公共十进制数{get;set;}
公共字符串ClientId{get;set;}
}
公开课成绩
{
公共字符串CustomerId{get;set;}
公共字符串OrderType{get;set;}
public DateTime OrderDate{get;set;}
public bool?已批准{get;set;}//添加null属性
公共IEnumerable数据{get;set;}
}