tags:

views:

110

answers:

3

Is there a quick way to say

Where (data.x == (If notempty x = y else x = default))

Assuming the data being compared is both strings

Compare listitem.string with passed.string - if passed.string isnotempty, else passed.string equals default value.

in a Linq 'where` query.

Apologies that the question was really badly written.

... As per the comments updated ...

+3  A: 

It really depends on the data type of x and y, but say they are strings:

Where (data.x == (!string.IsNullOrEmpty(y) ? y : "default"))

It's called the conditional operator.

Fredrik Mörk
This is probably close to what the OP wants. Note however that evaluating `!foo` in a conditional (`!string.IsNullOrEmpty(y)` here) is considered bad practice. Much simpler just to switch the order of your true/false return values. :)
Noldorin
@Noldorin: I know, just didn't want to swap things around to much in comparison to the snippet in the question; good point to raise still, thanks for that.
Fredrik Mörk
Yeah, fair enough. For consistency, it probably makes sense to leave it this way. (It also corresponds more to the null coalescing operator.)
Noldorin
If this is the case, and `y` is a "passed string", then it would be better to set `if (string.IsNullOrEmpty(y)) y = "defaultValue";` before executing the Linq query. This is part of parameter validation.
280Z28
+3  A: 

Your original post could be interpreted to be this:

where (data.x ?? y) == y

Edit: I am now pretty sure this is what you were attempting. :)

Edit 2: This is the null-coalescing operator.

Edit 3: Whoever voted me down should post a reason. I interpreted the first post as: "If x is not empty, it should equal y, but it's ok to equal default(typeof y)." For reference types or nullable types, my post is correct.

Edit 4: Mine still could be the cleanest answer to his updated post. I keep reading it and it's still strangely worded. The (string.IsNullOrEmpty(y)) post is the other possibility.

280Z28
Admittedly the question isn't very clear, but I highly doubt this is what the OP wants. You've got x and y the wrong way round for a start.
Noldorin
Check my edit. This wording makes sense in the original post, and here's how it might be used. "If the user entered option *y*, do something. Also, *y* is the default, so if they didn't enter anything, that's ok too." That's just one example.
280Z28
There's no indication that this "default" value is `y`. In fact, quite the opposite - I don't see how it could be.
Noldorin
"default" would refer to the type of both `data.x` and `y`. It's a `where` clause, so I have to assume he didn't mean assignment.
280Z28
The `=` in the clause definitely means assignment, since he's using `==` for equality, which is standard. `x` would just seem to be a temporary variable. The syntax is slightly ambiguous though, I would concede.
Noldorin
That's assuming it was perfectly written in the OP. You have to read through things when they're clearly focusing on concepts over syntax.
280Z28
+3  A: 

Ok, with your clarifications to the original post, I think I see what you're getting at now.

Try the following clause (where data.x is your current element and y in the passed argument).

where y == (string.IsNullOrEmpty(y) ? "default" : data.x)

I do not believe it's possible to use the null coalescing operator here, since the variables you are comparing and returning are different.

Old Post:
If what is empty? I'm guessing you're referring to y.

For a generic type T, you could use the following clause:

where data.x == (y ?? DefaultValue<T>())

where DefaultValue is a generic function of T that returns a so-called "default" value.

Noldorin
`(y ?? default(T))` is `(y != default(T) ? y : default(T))` which is just `y`
280Z28
Good point. It should just be an arbitrary function rather than the `default` keyword.
Noldorin
Ah, seems I interpreted it correctly this time. :)
Noldorin
Yep thanks to all on that one; and as well to 280Z28 for the MSDN link http://msdn.microsoft.com/en-us/library/ms173224.aspx
Chris M
Fine, I'll concede this one to you +1. ;) I do find this type of question entertaining though, almost more so than ones with a straight answer because a gambling factor is involved. :)
280Z28
@280Z28: Hehe, thanks. And yeah, the question was somewhat exciting (at least quite atypical) in that we were effectively placing our bets on the possibilities...
Noldorin