Reputation: 63
This is my code.
[HttpPost]
public ActionResult TopProjects(FormCollection form)
{
bool top, desgin, implement;
string str = "";
if (string.IsNullOrEmpty(form["top"])) { top = false; } else { top = true;str += "x=>x.IsTopProject==top"; }
if (string.IsNullOrEmpty(form["desgin"])) { desgin = false; } else { desgin = true; str += "&& x.IsAnDesigning==desgin"; }
if (string.IsNullOrEmpty(form["implement"])) { implement = false; } else { implement = true;str += "&& x.IsAnImplementation==implement"; }
return View(blProject.Select().Where(str));
}
The lambda get error from str. How can i solving it?
Upvotes: 0
Views: 50
Reputation: 30464
Apparently blProject
an object of a class that implements IQueryable of some class, let's say it implements IQueryable<BlProjectItem>
:
IQueryable<BlProjectItem> blProjects = ...
Apparently every BlProjectItem has at least three boolean properties:
class BlProjectItem
{
public bool IsTopProject {get; set;}
public bool IsAnDesigning {get; set;}
public bool IsAnImplementation {get; set;}
...
}
You define three Booleans in a difficult way, you meant to say:
bool top = String.IsNullOrEmpty(form["top"]));
bool design = String.IsNullOrEmpty(form["desgin"]);
bool implement = String.IsNullOrEmpty(form["implement"]);
Depending on whether on the values of top / design / implement this is what you want
Top Design Implement All BlProjectItems where
0 0 0 !IsTopProject && !IsAnDesigning && !IsAnImplementation
0 0 1 !IsTopProject && !IsAnDesigning && IsAnImplementation
0 1 0 !IsTopProject && IsAnDesigning && !IsAnImplementation
0 1 1 !IsTopProject && IsAnDesigning && IsAnImplementation
1 0 0 IsTopProject && !IsAnDesigning && !IsAnImplementation
1 0 1 IsTopProject && !IsAnDesigning && IsAnImplementation
1 1 0 IsTopProject && IsAnDesigning && !IsAnImplementation
0 1 1 IsTopProject && IsAnDesigning && IsAnImplementation
In other words you want:
.Where(item => item.IsTopProject == top
&& item.IsAnDesigning == design
&& item.IsAnImplementation == implement)
Your function will be like:
public ActionResult TopProjects(FormCollection form)
{
bool top = String.IsNullOrEmpty(form["top"]));
bool design = String.IsNullOrEmpty(form["desgin"]);
bool implement = String.IsNullOrEmpty(form["implement"]);
var query = blProject.Where(item => item.IsTopProject == top
&& item.IsAnDesigning == design
&& item.IsAnImplementation == implement)
// only if you don't want all properties of BlProjectItem:
.Select(item => new
{
// Select the items that you want, e.g.
Id = item.Id,
Name = item.Name,
...
});
return view(Query);
}
Upvotes: 1