Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
I need a ternary operator in where clause linq.

What I have tried:

C#
filteredItem = ItemList.Where(x =>; (Model != "" ? (x.ModelNumber.ToLower().Contains(Model.ToLower())) : x.ModelNumber)
Posted
Updated 24-Aug-16 1:15am
v2
Comments
Kornfeld Eliyahu Peter 14-Mar-16 10:44am    
And what is wrong with the one you have?
Sascha Lefèvre 14-Mar-16 10:50am    
Please explain your intention - what do you want to achieve here?
Philippe Mori 7-May-16 15:46pm    
As it, your code would not even compile. The ; after the arrow => does not make any sense.

And by the way, you don't even tell problem nor what you want to do. As such, you don't deserve many points as you don't make much effort to write adequate question.
divya behera 30-Aug-16 2:12am    
please specify your requirement

In a ternary operator, both elements in each side of the : must return the same type.
Which is apparently not the case in your code, where the left part returns a boolean (Contains method) whereas the right part seems to return a string (ModelNumber).

Please see ?: Operator (C# Reference)[^].
Especially this paragraph:
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.
 
Share this answer
 
Comments
Angelo L 1-Apr-16 12:26pm    
This is correct, doubly so, since the where operator requires that the lambda return a boolean.
That doesn't make any sense.

The code you posted does this:
C#
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

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.
 
Share this answer
 
Comments
Sascha Lefèvre 14-Mar-16 11:08am    
The compiler won't allow it in the first place.
Dave Kreskowiak 14-Mar-16 12:26pm    
I know. I just left it as is because the OP hasn't really described the problem or what he wants in sufficient detail.
From your code I am guessing that you are trying to filter and modify your code at the same time, which usually is not possible to do out of the box.

If I understood you snippet correctly you want to return a "ModelNumber" property if "Model" is not empty and it is contained it currently iterated element.

Here is a possible solution:

C#
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!
 
Share this answer
 
var output=myList.FirstOrDefault(x => X.ModelNumber != null ? x.ModelNumber.ToLower().Contains(Model.ToLower()) : x.ModelNumber))
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900