Biography:
Location: Dayton, OHInterests:
Music, Playing Guitar, Working on Cars, Home Improvement, Photography, Computers, Website Programming, Driving in the snow, NHL Hockey, Sand Volleyball, Settlers of Catan board gameFavorites:
Foods: Lasagna, Carrot Casserole, Chicken ParmesanEmployment Information:
Current Resume: Download .docFun links you should probably check out:
Fun games you might want to play:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RubberPhotos v.90 by Justin Tubbs |
Current Code Samples:
Future Code Samples:
I have a search form in my WPF Application that needs to have 8 input boxes/criteria for searches. Some are dropdowns, others are textboxes, and I am assured that each input can only have one value. (This example breaks if you want multiple inputs I think). Since I can be assured that each input can only have one value, I can use some boolean/ternary operators to selectively filter what belongs in the WHERE clause of my LINQ statement.

First, I made a simple public ENUM in my application's namespace to store the values I wanted to use for MatchType:

Then, I wrote some simple code to retrieve the values from my input form and save them to local variables:

Finally, I wrote a pretty massive LINQ query using the ternary operator syntax: (I'll explain the logic below)

Ok, so the where clause of this LINQ statement is pretty complex, but I hope to explain this clearly enough to make it understandable for everyone.
This line uses basic ternary operator syntax to state that if the PartsBookTitle variable has a length of 0 characters that we return boolean TRUE, otherwise we continue to the next condition (which is wrapped onto the next line)
This 2nd line of the where clause is only fired when the first line of the where clause returns boolean FALSE. This line will use another ternary operator to evaluate the mtPartsBookTitle variable to see if it matches our MatchType ENUM of 1. If so, then the first part of the ternary operation will fire, checking the <titles> element's child element <title> where it's Value is equal to the PartsBookTitle string variable. However, if mtPartsBookTitle is not MatchType of 1, then we continue down to the 3rd line of the where clause.
Hopefully you can see how I'm simply repeating this logic flow for MatchTypes of 2, 3, and 4. I'm also reproducing this logic for other input form variables such as ModelNumber and MediaNumber. Other input fields (that do not have multiples in my XML document) are handled slightly differently. Notice how Region and Brand variables are checked with a more simple variant of this ternary operation.
Go Faster:
Stop Faster:
Handle Better:
Drive Smoother:
Save Weight:
Look Better:
Sound Louder:
Subaru WRX/STI Websites:
Subaru Tuning Companies/Parts Retailers:
Drywall and Framing:
Insulation Instructions:
Home Theater Install, Setup, & Configuration:
Friends and Family:
Businesses I Like:
| copyright © 2002-2010 Justin Tubbs | email the webmaster |