C# 有没有办法在linq查询的select部分执行循环?
我正在返回用户的佣金文档。CommissionDocument包含许多用户获得佣金的策略 因此,我想在佣金文件中总结每项政策的佣金 我尝试过使用Sum()方法,但没有成功 质疑 委托制图课C# 有没有办法在linq查询的select部分执行循环?,c#,linq,C#,Linq,我正在返回用户的佣金文档。CommissionDocument包含许多用户获得佣金的策略 因此,我想在佣金文件中总结每项政策的佣金 我尝试过使用Sum()方法,但没有成功 质疑 委托制图课 public class AgentCommission { public int AgentId { get; set; } public string FileUrl { get; set; } public Guid? FileGuid { get; set; }
public class AgentCommission
{
public int AgentId { get; set; }
public string FileUrl { get; set; }
public Guid? FileGuid { get; set; }
public DateTime ScanDate { get; set; }
public Decimal? Commission { get; set; }
}
谢谢你的帮助和建议。 下面是我想做的。 我得到了佣金的总额
public async Task<List<Commission>> FetchAsync(Agent agent)
{
return await _agentsContext.ScanCommDoc
.Include(x => x.CommissionStatement)
.Where(x => x.AgentId == agent.AgentId && x.Type == FileType.CommissionStatement
&& x.CommissionStatement.PaymentYear == x.ScanDate.AddMonths(-1).Year
&& x.CommissionStatement.PaymentMonth == x.ScanDate.AddMonths(-1).Month)
.Select(x => new { x.ScanDate, x.FileUrl, x.FileGuid, commission = (x.CommissionStatement.Amount + x.CommissionStatement.Vat) })
.GroupBy(x => new { x.FileGuid, x.FileUrl, x.ScanDate })
.Select(p => new AgentCommission
{
FileGuid = p.Key.FileGuid,
FileUrl = p.Key.FileUrl,
ScanDate = p.Key.ScanDate,
Commission = p.Sum(x => x.commission)
})
.OrderByDescending(x => x.ScanDate).Take(10).ToListAsync();
}
公共异步任务FetchAsync(代理)
{
返回等待_agentContext.scanComdoc
.包括(x=>x.佣金声明)
其中(x=>x.AgentId==agent.AgentId&&x.Type==FileType.CommissionStatement
&&x.CommissionStatement.PaymentYear==x.ScanDate.AddMonths(-1).年
&&x.CommissionStatement.PaymentMonth==x.ScanDate.AddMonths(-1.Month)
.Select(x=>new{x.ScanDate,x.FileUrl,x.FileGuid,佣金=(x.CommissionStatement.Amount+x.CommissionStatement.Vat)})
.GroupBy(x=>new{x.FileGuid,x.FileUrl,x.ScanDate})
.选择(p=>new AgentCommission
{
FileGuid=p.Key.FileGuid,
FileUrl=p.Key.FileUrl,
ScanDate=p.Key.ScanDate,
佣金=p.Sum(x=>x.Commission)
})
.OrderByDescending(x=>x.ScanDate).Take(10).toListSync();
}
感谢您的帮助和建议。
下面是我想做的。
我得到了佣金的总额
public async Task<List<Commission>> FetchAsync(Agent agent)
{
return await _agentsContext.ScanCommDoc
.Include(x => x.CommissionStatement)
.Where(x => x.AgentId == agent.AgentId && x.Type == FileType.CommissionStatement
&& x.CommissionStatement.PaymentYear == x.ScanDate.AddMonths(-1).Year
&& x.CommissionStatement.PaymentMonth == x.ScanDate.AddMonths(-1).Month)
.Select(x => new { x.ScanDate, x.FileUrl, x.FileGuid, commission = (x.CommissionStatement.Amount + x.CommissionStatement.Vat) })
.GroupBy(x => new { x.FileGuid, x.FileUrl, x.ScanDate })
.Select(p => new AgentCommission
{
FileGuid = p.Key.FileGuid,
FileUrl = p.Key.FileUrl,
ScanDate = p.Key.ScanDate,
Commission = p.Sum(x => x.commission)
})
.OrderByDescending(x => x.ScanDate).Take(10).ToListAsync();
}
公共异步任务FetchAsync(代理)
{
返回等待_agentContext.scanComdoc
.包括(x=>x.佣金声明)
其中(x=>x.AgentId==agent.AgentId&&x.Type==FileType.CommissionStatement
&&x.CommissionStatement.PaymentYear==x.ScanDate.AddMonths(-1).年
&&x.CommissionStatement.PaymentMonth==x.ScanDate.AddMonths(-1.Month)
.Select(x=>new{x.ScanDate,x.FileUrl,x.FileGuid,佣金=(x.CommissionStatement.Amount+x.CommissionStatement.Vat)})
.GroupBy(x=>new{x.FileGuid,x.FileUrl,x.ScanDate})
.选择(p=>new AgentCommission
{
FileGuid=p.Key.FileGuid,
FileUrl=p.Key.FileUrl,
ScanDate=p.Key.ScanDate,
佣金=p.Sum(x=>x.Commission)
})
.OrderByDescending(x=>x.ScanDate).Take(10).toListSync();
}
所以您尝试了.ToList()。选择(x=>x.table.Amount).sum()?我看不出在代码中使用了sum()
,但在FileUrl=x.FileUrl
@terrencep之后缺少逗号,不确定您在查询中指的是哪里。您能使用上面的代码吗?@Henrik我在选择代理委托对象之后尝试过使用它。在我的问题中包含代码时,逗号只是一个输入错误。您是否可以确保您的代码已编译,或者添加一条注释,显示您想要的Sum
?另外,您引用了“policy”,但我在代码中没有看到policy对象。选择(x=>x.table.Amount).sum()?我看不出在代码中使用了sum()
,但在FileUrl=x.FileUrl
@terrencep之后缺少逗号,不确定您在查询中指的是哪里。您能使用上面的代码吗?@Henrik我在选择代理委托对象之后尝试过使用它。在我的问题中包含代码时,逗号只是一个输入错误。您是否可以确保您的代码已编译,或者添加一条注释,显示您想要的Sum
?另外,您提到了“policy”,但我在代码中没有看到policy对象。
public class ScanCommDoc
{
public int Id { get; set; }
public int AgentId { get; set; }
public string FileUrl { get; set; }
public Guid? FileGuid { get; set; }
public PolicyDetail Policy { get; set; }
public FileType Type { get; set; }
public DateTime ScanDate { get; set; }
public CommissionStatement CommissionStatement { get; set; }
}
public async Task<List<Commission>> FetchAsync(Agent agent)
{
return await _agentsContext.ScanCommDoc
.Include(x => x.CommissionStatement)
.Where(x => x.AgentId == agent.AgentId && x.Type == FileType.CommissionStatement
&& x.CommissionStatement.PaymentYear == x.ScanDate.AddMonths(-1).Year
&& x.CommissionStatement.PaymentMonth == x.ScanDate.AddMonths(-1).Month)
.Select(x => new { x.ScanDate, x.FileUrl, x.FileGuid, commission = (x.CommissionStatement.Amount + x.CommissionStatement.Vat) })
.GroupBy(x => new { x.FileGuid, x.FileUrl, x.ScanDate })
.Select(p => new AgentCommission
{
FileGuid = p.Key.FileGuid,
FileUrl = p.Key.FileUrl,
ScanDate = p.Key.ScanDate,
Commission = p.Sum(x => x.commission)
})
.OrderByDescending(x => x.ScanDate).Take(10).ToListAsync();
}