asp.net mvc entity framework conditions
Such a question In C#, I used the{[4] class]}
SQLiteConnection myConnection = new SQLiteConnection(Connect);
myConnection.Open();
The query string was constructed from the selection conditions
if (chk0.Checked) { param1 = " AND fire = 0 "; }
else if (chk1.Checked) { param1 = " AND fire = 1 "; }
else {param1 = " "}
if (chk3.Checked) { param2 = " AND str = 0 "; }
else if (chk4.Checked) { param2 = " AND str = 1 "; }
else {param2 = " "}
Connect = "SELECT * FROM item WHERE " + param1 + param2 ;
As you can see, the query in particular WHERE is based on the results of if
Question? How to achieve the same result using entity framework
At the moment, I create a separate request in each if condition, which is very inconvenient because there are quite a lot of parameters.
if (vulnerability != -100 && raite == "-100")
{
return from Name in northwind.name
where class_tip_list.Contains(Grp.class)
select new { тут выборка };
}
else
{
return from Name in northwind.name
where Grp.level >= slider_min
select new { тут выборка };
}
0
1 answers
Something like:
var query = items.AsQueryable();
if(chk0.Checked)
{
query=query.Where(x=>x.Fire);
}
var result = query.ToList();
2
Author: Иван Навознов, 2013-02-28 06:40:52