本文介绍了linq的三元运营商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个三元运算符where where子句linq。
我尝试过:
I need a ternary operator in where clause linq.
What I have tried:
filteredItem = ItemList.Where(x =>; (Model != "" ? (x.ModelNumber.ToLower().Contains(Model.ToLower())) : x.ModelNumber)
推荐答案
Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.
希望这会有所帮助。
Hope this helps.
if Model does not equal ""
// This will return a boolean value.
return (x.ModelNumber.ToLower().Contains(Model.ToLower())
else
// This will return whatever ModelNumber is.
return x.ModelNumber
我非常怀疑你的ModelNumber是一个布尔值,所以你的两个返回类型是不同的。在三元操作中,这是一个灾难的处方。
I highly doubt your ModelNumber is a boolean so your two return types are different. In a Ternary operation, that is a recipe for disaster.
var filteredItems =
string.IsNullOrWhiteSpace(Model)
? ItemList
: ItemList.Where(x => x.ModelNumber.ToLowerInvariant().Contains(Model.ToLowerInvariant())).ToList();
希望这会有所帮助,祝你好运,下次请尝试更具体,或向我们展示更多代码。
干杯!
Hope this helps, good luck and next time please try to be more specific, or show us more code.
Cheers!
这篇关于linq的三元运营商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!