LINQ and dynamic where clauses

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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); }
}

.



Relevant Pages

  • RE: LINQ and dynamic where clauses
    ... it seems that I do not need to cast my Session object to an IQueryable ... (actually an extension method) ... IQueryableemployees = ... switch ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: would C be easier to read if...
    ... design feature that syntax for declaring a variable is the same as for ... And hopefully 'parameter' is a non-const pointer so the void* cast ... mutate what is pointed to by parameter. ... On the other hand the const may be cast away, ...
    (comp.lang.c)
  • Re: Today is the International Lefthanders Day!
    ... He didn't think to switch to his other hand. ... handwriting came out pretty much the same either way. ... I had a friend in college like that. ... I did, however, cut a slot in the cast that would accommodate a guitar ...
    (rec.music.beatles)
  • Re: OT: Multiball! (Re: Orbquest)
    ... When you got in trouble you'd switch to the wizard ... the entire party by a single @ and noting who's in charge at any given ... So the character can be programmed to cast ... Players can also pause the game and manually input commands. ...
    (rec.games.roguelike.development)
  • Re: Language enhancements
    ... your lists won't usually match the type of your items (especially in the ... them without the explicit cast. ... using the syntax of casting the /collection/ to imply that the individual ...
    (borland.public.delphi.non-technical)