Linq Join,按多个字段分组
Linq Join, group by multiple field
我有两个table如下
public class Customer
{
public string Id{ get; set; }
public string Name { get; set; }
[DataType(DataType.Currency), Column(TypeName = "decimal(18,2)")]
public decimal Balance { get; set; }
[Display(Name ="Time Registered")]
public DateTime TimeRegistered { get; set; }
[InverseProperty("Beneficiary")]
public ICollection<WalletTransaction> TransactionsReceived { get; set; }
[InverseProperty("Sender")]
public ICollection<WalletTransaction> TransactionsInitiated { get; set; }
}
和
public class WalletTransaction
{
public int Id { get; set; }
public TransactionType TrasactionType { get; set; }
public decimal Amount { get; set; }
public string SenderId { get; set; }
public string ReceipientId { get; set; }
[ForeignKey("SenderId")]
public Customer Sender { get; set; }
[ForeignKey("ReceipientId")]
public Customer Beneficiary { get; set; }
}
和交易类型可以在以下枚举中
public enum TransactionType
{
Deposit=1,Transfer=2,Withdrawal=3
}
现在我需要获取每个客户交易的摘要,细分为不同的交易类型,如下所示:
Name of CustomerA
Deposit=Sum of the amount value in all his transactions of transaction type Deposit
Transfer=Sum of the amount value in all his transactions of transaction type Transfer
Withdrawal=Sum of the amount value in all his transactions of transaction type Withdrawal
Name of CustomerB
Deposit=Sum of the amount value in all his transactions of transaction type Deposit
Transfer=Sum of the amount value in all his transactions of transaction type Transfer
Withdrawal=Sum of the amount value in all his transactions of transaction type Withdrawal
Name of CustomerC
Deposit=Sum of the amount value in all his transactions of transaction type Deposit
Transfer=Sum of the amount value in all his transactions of transaction type Transfer
Withdrawal=Sum of the amount value in all his transactions of transaction type Withdrawal
etc.
我不知道如何实现
我的想法是我需要创建 linq 查询来加入客户 table 和交易 table 以从客户 table 中获取客户的姓名。然后,按客户和交易类型对交易进行分组。我不确定这是否正确,但即使是这样,我也不知道该怎么做。
我试过这样做
var customerTransactions = await _context.Customers
.Include(c => c.TransactionsInitiated)
.Select(a => new
{
//Looking for how to break down he transactions by the transaction types here
})
.OrderByDescending(s=>s.TotalAmount)//Not sure too how to order the result
.ToListAsync();
请指导我如何解决这些问题。
谢谢
由于您具有导航属性,因此在 LINQ 语句中不需要 Join
。基本上你想要的可以在
这样的查询中完成
_context.Customers
.Select(a => new
{
c.Name,
Deposit = c.TransactionsInitiated
.Where(t => t.TransactionType == TransactionType.Deposit).Sum(t => t.Amount),
Transfer = c.TransactionsInitiated
.Where(t => t.TransactionType == TransactionType.Transfer).Sum(t => t.Amount),
Withdrawal = c.TransactionsInitiated
.Where(t => t.TransactionType == TransactionType.Withdrawal).Sum(t => t.Amount),
TotalAmount = c.TransactionsInitiated
.Sum(t => t.Amount),
})
.OrderByDescending(s => s.TotalAmount)
...并且 EF 将加入生成的 SQL 语句。
免责声明:EF 核心 3 在将更复杂的查询转换为 SQL 方面非常有限,我不确定这是否真的有效,而且我手头没有类似的测试用例。
我有两个table如下
public class Customer
{
public string Id{ get; set; }
public string Name { get; set; }
[DataType(DataType.Currency), Column(TypeName = "decimal(18,2)")]
public decimal Balance { get; set; }
[Display(Name ="Time Registered")]
public DateTime TimeRegistered { get; set; }
[InverseProperty("Beneficiary")]
public ICollection<WalletTransaction> TransactionsReceived { get; set; }
[InverseProperty("Sender")]
public ICollection<WalletTransaction> TransactionsInitiated { get; set; }
}
和
public class WalletTransaction
{
public int Id { get; set; }
public TransactionType TrasactionType { get; set; }
public decimal Amount { get; set; }
public string SenderId { get; set; }
public string ReceipientId { get; set; }
[ForeignKey("SenderId")]
public Customer Sender { get; set; }
[ForeignKey("ReceipientId")]
public Customer Beneficiary { get; set; }
}
和交易类型可以在以下枚举中
public enum TransactionType
{
Deposit=1,Transfer=2,Withdrawal=3
}
现在我需要获取每个客户交易的摘要,细分为不同的交易类型,如下所示:
Name of CustomerA
Deposit=Sum of the amount value in all his transactions of transaction type Deposit
Transfer=Sum of the amount value in all his transactions of transaction type Transfer
Withdrawal=Sum of the amount value in all his transactions of transaction type Withdrawal
Name of CustomerB
Deposit=Sum of the amount value in all his transactions of transaction type Deposit
Transfer=Sum of the amount value in all his transactions of transaction type Transfer
Withdrawal=Sum of the amount value in all his transactions of transaction type Withdrawal
Name of CustomerC
Deposit=Sum of the amount value in all his transactions of transaction type Deposit
Transfer=Sum of the amount value in all his transactions of transaction type Transfer
Withdrawal=Sum of the amount value in all his transactions of transaction type Withdrawal
etc.
我不知道如何实现 我的想法是我需要创建 linq 查询来加入客户 table 和交易 table 以从客户 table 中获取客户的姓名。然后,按客户和交易类型对交易进行分组。我不确定这是否正确,但即使是这样,我也不知道该怎么做。
我试过这样做
var customerTransactions = await _context.Customers
.Include(c => c.TransactionsInitiated)
.Select(a => new
{
//Looking for how to break down he transactions by the transaction types here
})
.OrderByDescending(s=>s.TotalAmount)//Not sure too how to order the result
.ToListAsync();
请指导我如何解决这些问题。 谢谢
由于您具有导航属性,因此在 LINQ 语句中不需要 Join
。基本上你想要的可以在
_context.Customers
.Select(a => new
{
c.Name,
Deposit = c.TransactionsInitiated
.Where(t => t.TransactionType == TransactionType.Deposit).Sum(t => t.Amount),
Transfer = c.TransactionsInitiated
.Where(t => t.TransactionType == TransactionType.Transfer).Sum(t => t.Amount),
Withdrawal = c.TransactionsInitiated
.Where(t => t.TransactionType == TransactionType.Withdrawal).Sum(t => t.Amount),
TotalAmount = c.TransactionsInitiated
.Sum(t => t.Amount),
})
.OrderByDescending(s => s.TotalAmount)
...并且 EF 将加入生成的 SQL 语句。
免责声明:EF 核心 3 在将更复杂的查询转换为 SQL 方面非常有限,我不确定这是否真的有效,而且我手头没有类似的测试用例。