是否有可能在LINQ中创建三个具有三种不同结果的条件? [英] Is it possible to create three condtions in LINQ with three different results?

查看:74
本文介绍了是否有可能在LINQ中创建三个具有三种不同结果的条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LINQ中的三个或多个条件



我尝试过:



以下是我的LINQ:



 返回  await  _trialBalanceContext 
.Entries
.Select(x = > new
{
accountTitle = x.AccountTitle,
debitAmount = x.DebitAmount,
creditAmount = x.CreditAmount ,
})
.ToListAsync();



我想在上面的LINQ中应用以下条件。



  if (accountTitle ==  资产){
debitAmount = debitAmount * 100
creditAmount = creditAmount * 100
} else if (accountTitle == 费用){
debitAmount = debitAmount - creditAmount
creditAmount = creditAmount * debitAmount
} else
{
debitAmount = creditAmount + 30
creditAmount = debitAmount + 50
}

解决方案

最好的方法是将它们变成函数:

  public   int  GetDebit(MyClass x)
{
if (x。 AccountTitle == 资产返回 x.DebitAmount * 100 ;
else if (x.AccountTitle == 费用 return x.DebitAmount - x.CreditAmount;
return x.CreditAmount + 30 ;
}
public int GetCredit(MyClass x)
{
if (x.AccountTitle == 资产 return x.CreditAmount * 100 ;
else if (x.AccountTitle == 费用 return x.DebitAmount * x.CreditAmount;
return x.DebitAmount + 50 ;
}

然后只需使用它们:

 ....选择(x = >   new  
{
accountTitle = x.AccountTitle,
debitAmount = GetDebit(x),
creditAmount = GetCredit(x)
})....


您是否尝试过使用[? :]条件运算符?

  return   await  _trialBalanceContext 
.Entries
。选择(x = > new
{
accountTitle = x.AccountTitle,
debitAmount = x.AccountTitle == 资产 ?x.DebitAmount * 100 :x.AccountTitle == 费用?x.DebitAmount - x.CreditAmount:x.CreditAmount + 30
creditAmount = x.AccountTitle == 资产?x.CreditAmount * 100 :x .AccountTitle == 费用?x.DebitAmount * x.CreditAmount:x.DebitAmount + 50
})
.ToListAsy nc();





更多:?:Operator(C#Reference)| Microsoft Docs [ ^ ]


Three or multiple conditons in LINQ

What I have tried:

Below is my LINQ:

return await _trialBalanceContext
	.Entries
	.Select(x => new
	{
		accountTitle = x.AccountTitle,
		debitAmount = x.DebitAmount,
		creditAmount = x.CreditAmount,
	})
	.ToListAsync();


And I want to apply this conditions below in my LINQ above.

if (accountTitle == "Asset"){
 	debitAmount = debitAmount * 100
 	creditAmount = creditAmount * 100
 } else if (accountTitle == "Expense"){
 	debitAmount = debitAmount - creditAmount
 	creditAmount = creditAmount * debitAmount
 } else 
 {
 	debitAmount = creditAmount + 30
 	creditAmount = debitAmount + 50
 }

解决方案

Neatest way is to make them into functions:

public int GetDebit(MyClass x)
    {
    if (x.AccountTitle == "Asset") return x.DebitAmount * 100;
    else if (x.AccountTitle == "Expense") return x.DebitAmount - x.CreditAmount;
    return x.CreditAmount + 30;
    }
public int GetCredit(MyClass x)
    {
    if (x.AccountTitle == "Asset") return x.CreditAmount * 100;
    else if (x.AccountTitle == "Expense") return x.DebitAmount * x.CreditAmount;
    return x.DebitAmount + 50;
    }

Then just use them:

....Select(x => new
            {
                accountTitle = x.AccountTitle,
                debitAmount = GetDebit(x),
                creditAmount = GetCredit(x)
            })....


Have you tried to use [? :] conditional operator?

return await _trialBalanceContext
.Entries
.Select(x => new
{
accountTitle = x.AccountTitle,
debitAmount = x.AccountTitle == "Asset" ?  x.DebitAmount * 100 : x.AccountTitle == "Expense" ? x.DebitAmount - x.CreditAmount : x.CreditAmount + 30,
creditAmount = x.AccountTitle == "Asset" ? x.CreditAmount * 100: x.AccountTitle == "Expense" ? x.DebitAmount * x.CreditAmount : x.DebitAmount + 50
})
.ToListAsync();



More: ?: Operator (C# Reference) | Microsoft Docs[^]


这篇关于是否有可能在LINQ中创建三个具有三种不同结果的条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆