M
Mike Collins
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); }
}
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); }
}