tags:

views:

33

answers:

1

Hello everyone! I'd like to make a search function that takes indata from 5 textboxes, Name, gender,ID, animal category and animal. The diffrances between animal category and animal is for eg. animal category = mammal and animal = bear. So these are optional to the user, when he/she hits the button it should search for the given parameters. The data is saved in an genereic list with the Animal type. eg. ListanimalCollection

I tried to use linq, my query ->

    ienumerable<Animal> result= 
    from a in animalCollection where a.Name== myParameterName
    &&
    a.Gender == myParameterGender
    select a;

the problem comes to when user wants to have one or more than two parameters cus i don't know how to make the query depending on user input. Do i have to make a bunch of if-statements to check user input? I hope there is another way!

Im asking you smart experts for help with this! Hope i made myself clear enough.

Daniel, sweden

+2  A: 

Assuming that your parameters are all strings then you could do something like this:

var result = from a in animalCollection
             where (string.IsNullOrEmpty(myParameterName) || a.Name == myParameterName)
                && (string.IsNullOrEmpty(myParameterGender) || a.Gender == myParameterGender)
                && (string.IsNullOrEmpty(myParameterID) || a.ID == myParameterID)
                && (string.IsNullOrEmpty(myParameterCategory) || a.Category == myParameterCategory)
                && (string.IsNullOrEmpty(myParameterAnimal) || a.Animal == myParameterAnimal)
             select a;
LukeH
LukeH, Thank you for you quick respons.I tried your query and it does not work when one of the parameters is empty.eg, name="" and gender = female.Im out of sugestions, been trying to figure this out for quite some time now.Daniel
Daniel
@Daniel: It should work in that situation. What happens? Do you get an error? Does the query return no results? Unexpected results?
LukeH
You are totaly awesome!It really worked! Im so glad! I've spent like two days on trying for a good solution! This was exactly what i as looking for.Im sorry for accusing your solution to be wrong, i've tried so many.This is very nice!! Just a couple of rows! Awesome!So now the my search function works very well thanks to you!Just one question about this query...eg. (string.IsNullOrEmpty(myParameterName) || a.Name == myParameterName)How does this query work? If string.IsNullOrEmpty gets a match first, then it should not look up the name? Am i right? If false, then look up the name...
Daniel
LukeH
Okay! Im not sure that i really get it completly, allthough id like to. In the (string.IsNullOrEmpty(myParameterName) || a.Name == myParameterName)If the parameter is empty, then IsNullOrEmpty should return false, right? And a.Name == nameParameter should also return false then because it expects atleast some characters.So then (string.IsNullOrEmpty(myParameterName) || a.Name == myParameterName) would be (false || false). Allthough! One of those statements have to return true or otherwise the query would not continue to look for input in the other parameters, gender, id, cat and animal.
Daniel
@Daniel: No. If the parameter is *null* or *empty* then `string.IsNullOrEmpty` returns `true`, in which case the second part of the clause -- checking the property against the parameter -- doesn't get evaluated.
LukeH