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.Collecti ons.Generic.Lis t to type
System.Linq.IQu eryableâ€. 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<Busi nessLogic.Servi ces.UserProfile employees =
(IQueryable<Bus inessLogic.Serv ices.UserProfil e>)(List<Busine ssLogic.Service s.UserProfile>) Session["Employees"];
switch (ddSearchCriter ia.SelectedValu e)
{
case "UserName":
employees.Where (c =c.Username ==
txtSearchCriter ia.Text);
break;
case "Email":
employees.Where (c =c.EmailAddress ==
txtSearchCriter ia.Text);
break;
default: //LastName
employees.Where (c =c.Person.LastN ame ==
txtSearchCriter ia.Text);
break;
}
BindGrid(employ ees.Select(c =>
c).ToList<Busin essLogic.Servic es.UserProfile> ());
}
else
{
List<BusinessLo gic.Services.Us erProfileemploy ees = null;
Shared.Business Logic.UserSearc hFilter filter = new
UserSearchFilte r();
switch (ddSearchCriter ia.SelectedValu e)
{
case "UserName":
filter.Username = txtSearchCriter ia.Text;
break;
case "Email":
filter.EmailAdd ress = txtSearchCriter ia.Text;
break;
default: //LastName
filter.LastName = txtSearchCriter ia.Text;
break;
}
PagedResult<Bus inessLogic.Serv ices.UserProfil epagedResult =
SearchEmployees (filter);
employees =
pagedResult.Res ults.ToList<Bus inessLogic.Serv ices.UserProfil e>();
BindGrid(employ ees.ToList<Busi nessLogic.Servi ces.UserProfile >());
}
}
catch (ThreadAbortExc eption) { }
catch (Exception ex) { ExceptionHelper .Publish(ex); }
}
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.Collecti ons.Generic.Lis t to type
System.Linq.IQu eryableâ€. 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<Busi nessLogic.Servi ces.UserProfile employees =
(IQueryable<Bus inessLogic.Serv ices.UserProfil e>)(List<Busine ssLogic.Service s.UserProfile>) Session["Employees"];
switch (ddSearchCriter ia.SelectedValu e)
{
case "UserName":
employees.Where (c =c.Username ==
txtSearchCriter ia.Text);
break;
case "Email":
employees.Where (c =c.EmailAddress ==
txtSearchCriter ia.Text);
break;
default: //LastName
employees.Where (c =c.Person.LastN ame ==
txtSearchCriter ia.Text);
break;
}
BindGrid(employ ees.Select(c =>
c).ToList<Busin essLogic.Servic es.UserProfile> ());
}
else
{
List<BusinessLo gic.Services.Us erProfileemploy ees = null;
Shared.Business Logic.UserSearc hFilter filter = new
UserSearchFilte r();
switch (ddSearchCriter ia.SelectedValu e)
{
case "UserName":
filter.Username = txtSearchCriter ia.Text;
break;
case "Email":
filter.EmailAdd ress = txtSearchCriter ia.Text;
break;
default: //LastName
filter.LastName = txtSearchCriter ia.Text;
break;
}
PagedResult<Bus inessLogic.Serv ices.UserProfil epagedResult =
SearchEmployees (filter);
employees =
pagedResult.Res ults.ToList<Bus inessLogic.Serv ices.UserProfil e>();
BindGrid(employ ees.ToList<Busi nessLogic.Servi ces.UserProfile >());
}
}
catch (ThreadAbortExc eption) { }
catch (Exception ex) { ExceptionHelper .Publish(ex); }
}
Comment