c# - 使用实体框架的带有if参数的Groupby
问题描述
我的服务有一个枚举参数,我需要按参数值分组
我目前正在使用if
并尝试使用switch
我的导航属性有属性Name
使用if
或Switch
我复制我的代码
Select(x =>
我的模型:
public class Bill
{
public int Id {get;set;}
public int CustomerId {get;set;}
public Customer Customer {get;set;}
public int BankAccountId {get;set;}
public BankAccount BankAccount {get;set;}
public int PlanId {get;set;}
public Plan Plan {get;set;}
public string Description {get;set;}
public DateTime Date {get;set;}
public decimal Amount {get;set;}
}
我的服务返回一个 DTO
public class BillService
{
public BillDTO ReturnGroupBy(BillGroup group)
{
if (group== BillGroup.Customer)
{
return dbo.Bill.GroupBy(c => c.Customer).Select(c => new BillDTO { Nome = c.Nome })..
}
if(group== BillGroup.BankAccount)
{
return dbo.Bill.GroupBy(c => conta.BankAccount).Select(c => new BillDTO { Nome = c.Nome })..
}
}
}
解决方案
找到共同点并声明该类型的变量,在这种情况下IQueryable<Bill>
:
IQueryable<Bill> groupedBills;
if (group == BillGroup.Customer)
{
groupedBills = dbo.Bill.GroupBy(c => c.Customer);
}
else
{
groupedBills = dbo.Bill.GroupBy(c => conta.BankAccount);
}
return groupedBills.Select(c => new BillDTO
{
Nome = c.Nome,
// ...
});
推荐阅读
- database - 我将如何在按钮单击时从 Delphi 中的 MS Access 表字段填充 TDBEdit
- javascript - 从扩展的电子类调用类方法时出错
- javascript - src\components\cards.js Line 75:68: 'index' is not defined no-undef 搜索关键字以了解有关每个错误的更多信息
- google-bigquery - BQ 不会降低查询成本
- delphi - TFDManager v/s FDManager 函数与 TFDCustomManager
- amazon-web-services - 使用 apigClientFactory 生成后,Lambda 授权程序因令牌无效而失败
- metadata - 如何解决 Yocto 项目构建中的 Taskhash 不匹配和元数据不确定?
- ruby - 如何将 Rails 控制台提升为低权限的 www-data 用户?
- flutter - 当应用程序在后台 FCM 中时颤动显示对话框或页面
- mysql - 不要在sql中实现两个命令