I’m trying to create a Linq query based on a user’s input. Writing sql I could build a sql select string based upon the values selected but with linq to sql I’m not sure how to do it. I’m using linq to sql classes. Here’s what I have
// Locals
TalentDBDataContext talentDB = new TalentDBDataContext()
int minAge = 0;
int maxAge = 120;
bool maleChecked = false;
bool femaleChecked = false;
List<string> ethnicities = new List<string>();
List<string> states = new List<string>();
var formData = Request.Form;
foreach (string key in formData.AllKeys)
{
string val = formData[key];
switch(key)
{
case "AgeMinTxBx":
minAge = Int32.Parse(val);
break;
case "AgeMaxTxBx":
maxAge = Int32.Parse(val);
break;
case "GenderMaleCB":
maleChecked = true;
break;
case "GenderFemaleCB":
femaleChecked = true;
break;
case "EthnicitySelector":
ethnicities = val.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries).ToList();
break;
case "StateSelector":
states = val.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries).ToList();
break;
default:
break;
}
}
var results =
from t in talentDB.Talent
(femaleChecked == true) ? where t. // I don't thing this will work..
Is there a way I could do this? Hope this makes sense. I like the intellisense linq provides and the error checking as well. Perhaps there’s still a way to write regular sql select string, and I could do it in this situation. thanks
You can use the
ExecuteQuerymethod to directly execute a SQL query on that database.