LINQ and dynamic where clauses
- From: Mike Collins <MikeCollins@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 19 Sep 2008 07:56:02 -0700
I am trying to set up a dynamic search using linq. I believe the syntax is
correct, but cannot confirm it because when I try to cast my
Session[“Employees”] from a List<> to IQueryable<>, I get a cast error
“Unable to cast object of type System.Collections.Generic.List to type
System.Linq.IQueryable”. Is there a way to cast List<> to IQueryable<>, or is
there a different way I need to be doing this?
protected void btnSearch_Click(object sender, EventArgs e)
{
try
{
if (Session["Employees"] != null)
{
IQueryable<BusinessLogic.Services.UserProfile> employees =
(IQueryable<BusinessLogic.Services.UserProfile>)(List<BusinessLogic.Services.UserProfile>)Session["Employees"];
switch (ddSearchCriteria.SelectedValue)
{
case "UserName":
employees.Where(c => c.Username ==
txtSearchCriteria.Text);
break;
case "Email":
employees.Where(c => c.EmailAddress ==
txtSearchCriteria.Text);
break;
default: //LastName
employees.Where(c => c.Person.LastName ==
txtSearchCriteria.Text);
break;
}
BindGrid(employees.Select(c =>
c).ToList<BusinessLogic.Services.UserProfile>());
}
else
{
List<BusinessLogic.Services.UserProfile> employees = null;
Shared.BusinessLogic.UserSearchFilter filter = new
UserSearchFilter();
switch (ddSearchCriteria.SelectedValue)
{
case "UserName":
filter.Username = txtSearchCriteria.Text;
break;
case "Email":
filter.EmailAddress = txtSearchCriteria.Text;
break;
default: //LastName
filter.LastName = txtSearchCriteria.Text;
break;
}
PagedResult<BusinessLogic.Services.UserProfile> pagedResult =
SearchEmployees(filter);
employees =
pagedResult.Results.ToList<BusinessLogic.Services.UserProfile>();
BindGrid(employees.ToList<BusinessLogic.Services.UserProfile>());
}
}
catch (ThreadAbortException) { }
catch (Exception ex) { ExceptionHelper.Publish(ex); }
}
.
- Follow-Ups:
- RE: LINQ and dynamic where clauses
- From: bruce barker
- Re: LINQ and dynamic where clauses
- From: mesut
- RE: LINQ and dynamic where clauses
- Prev by Date: Re: How to remove localization(get rid of meta keys metaresourcekey)?
- Next by Date: Re: LINQ and dynamic where clauses
- Previous by thread: Problem with line breaks
- Next by thread: Re: LINQ and dynamic where clauses
- Index(es):
Relevant Pages
|