使用多个整数生成捕获速率时,C#LINQ除以零的错误
你知道我如何用除零来解决这个LINQ问题吗 我有一个对象列表,它给出了引用未完成的各种原因,并尝试按捕获率进行排序使用多个整数生成捕获速率时,C#LINQ除以零的错误,c#,linq,.net-4.5,C#,Linq,.net 4.5,你知道我如何用除零来解决这个LINQ问题吗 我有一个对象列表,它给出了引用未完成的各种原因,并尝试按捕获率进行排序 int NoOfQuotes //this is the number of quotes int NoOfQuotesBooked //these quotes completed int DuplicateQuotes //these are duplicate quotes that we don't want to count. 捕获率为noofkuotesbrocked
int NoOfQuotes //this is the number of quotes
int NoOfQuotesBooked //these quotes completed
int DuplicateQuotes //these are duplicate quotes that we don't want to count.
捕获率为noofkuotesbrocked/(noofkuotes-DuplicateQuotes)。
如果noofkuotes-DuplicateQuotes==0
,它应该返回排序的0.0
_customerSummaryItems = _customerSummaryItems.OrderByDescending(x => (x.NoOfQuotesBooked / (x.NoOfQuotes - x.DuplicateQuotes)))
.ThenBy(x => x.CompanyName)
.ThenBy(x => x.FirstName)
.ThenBy(x => x.LastName)
.ToList();
无论我如何尝试和排序,我都无法使Linq正确编译,因此我一定是把事情搞砸了。如何在这种排序中添加一个除零检查?因此只需检查结果并进行计算:
_customerSummaryItems = _customerSummaryItems
.OrderByDescending(x => x.NoOfQuotes - x.DuplicateQuotes != 0
? (x.NoOfQuotesBooked / (x.NoOfQuotes - x.DuplicateQuotes)))
: 0.0)
.ThenBy(x => x.CompanyName)
.ThenBy(x => x.FirstName)
.ThenBy(x => x.LastName)
.ToList();
似乎你想要:
_customerSummaryItems = _customerSummaryItems
.OrderByDescending(x =>
x.NoOfQuotes == x.DuplicateQuotes
? 0.0
: (x.NoOfQuotesBooked / (x.NoOfQuotes - x.DuplicateQuotes)))
.ThenBy(x => x.CompanyName)
.ThenBy(x => x.FirstName)
.ThenBy(x => x.LastName)
.ToList();
这是EF还是其他Linq提供程序(除Linq to对象外)?您遇到了什么编译器错误?这是关于为什么会出现除零错误的基本知识,这让我想知道您是否调试了它,或者只是执行了旧的
代码并运行了场景