Last post Oct 23, 2008 05:16 PM by renitdaniel
Sep 16, 2008 02:40 PM|renitdaniel|LINK
I have a few listboxes which hold data that serve as a search criteria. So, the search criteria data is selected within each list box and a search button is hit. On hitting search, a gridview is populated with the respective data that satisfies that search
criteria.I presently works for the selection of one entry in each listbox. I am trying to enable multiple selection within the list boxes and have it reflect on the gridview.
How could i accomplish through LINQ?
Im attaching the code i have used to get it working for a selection of just one criteria in each list box.
protected void doBinding()
IQueryable<vwProjectInfo> Projects = LinqUtil.GetResults<vwProjectInfo>(P => true);
if (lbAreas.SelectedIndex > 0)
Projects = Projects.Where(P => P.AreaFK == Int32.Parse(lbAreas.SelectedValue));
if (lbPMs.SelectedIndex > 0)
Projects = Projects.Where(P => P.ProjectSupervisorFK == Int32.Parse(lbPMs.SelectedValue));
if (lbGroups.SelectedIndex > 0)
Projects = Projects.Where(P => P.AreaGroupNameID == Int32.Parse(lbGroups.SelectedValue));
if (lbStatus.SelectedIndex > 0)
Projects = Projects.Where(P => P.statusFK == Int32.Parse(lbStatus.SelectedValue));
if (lbSponsor.SelectedIndex > 0)
Projects = Projects.Where(P => P.SponsorID == Int32.Parse(lbSponsor.SelectedValue));
if (lbPriority.SelectedIndex > 0)
Projects = Projects.Where(P => P.PriorityFK == Int32.Parse(lbPriority.SelectedValue));
if (sortExpression != null)
//ONLY WORKS FOR STRING COLUMNS!!!
//Programatcically build the lambda parameter - the P in P => P....
var vwProjectParam = Expression.Parameter(typeof(vwProjectInfo), "vwProjectInfo Parameter");
//Programatcically build the lambda body - the P.PropName in P => P.PropName - Expression.Convert is to handle value types, not needed here.
var convertedBody = Expression.Convert(Expression.Property(vwProjectParam, sortExpression), typeof(String));
//var convertedBody = Expression.Property(vwProjectParam, sortExpression);
Expression<Func<vwProjectInfo, String>> expr = Expression.Lambda<Func<vwProjectInfo, String>>(convertedBody, vwProjectParam);
Projects = this.SortDesc ? Projects.OrderByDescending(expr) : Projects.OrderBy(expr);
Projects = Projects.OrderBy(P => P.AreaGroupName).ThenBy(P => P.AreaName).ThenBy(P => P.ProjectName).ThenBy(P => P.statusName);
gvProjects.DataSource = Projects;
Oct 23, 2008 05:16 PM|renitdaniel|LINK
No one replied to this post, but i figured how to accomplish this by using the keyword "contains" in LINQ.