473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

LINQ and dynamic where clauses

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.UserProfileemplo yees =
(IQueryable<BusinessLogic.Services.UserProfile>)(L ist<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.UserProfileemployees = 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.UserProfilepage dResult =
SearchEmployees(filter);
employees =
pagedResult.Results.ToList<BusinessLogic.Services. UserProfile>();
BindGrid(employees.ToList<BusinessLogic.Services.U serProfile>());
}
}
catch (ThreadAbortException) { }
catch (Exception ex) { ExceptionHelper.Publish(ex); }
}

Sep 19 '08 #1
4 3578
Hi Mike, there are several ways of doing this. But I got this way
working...

I hope it helps,

cheers, mesut
objDataContext = DatabaseFactory.GetLinq();
List<Linq.tblCostLocalTable;
var query = (from costing in objDataContext.tblCosts
select costing);

//the where code below (2 lines) are
replace by dynamic where
//where costing.ProductID == productID &&
//costing.BusinessUnit == businessUnit

if (productID != 0) query = query.Where(cost =>
cost.ProductID == productID);
if (!string.IsNullOrEmpty(businessUnit)) query =
query.Where(cost =cost.BusinessUnit == businessUnit);

LocalTable = query.ToList();

return LocalTable;
Sep 19 '08 #2
Thanks for the reply, but I am not able to get it to work yet. Can you, or
anyone, offer some input.

Below is what I tried...also I decided that I needed to use SQLMethods.Like

List<.BusinessLogic.Services.UserProfileemployees =
(List<.BusinessLogic.Services.UserProfile>)Session["Employees"];
var query = from p in employees
select p;

switch (ddSearchCriteria.SelectedValue)
{
case "UserName":
query.Where(c =SqlMethods.Like(c.Username, "%" + txtSearchCriteria.Text
+ "%"));
break;

case "Email":
query.Where(c =SqlMethods.Like(c.EmailAddress, "%" +
txtSearchCriteria.Text + "%"));
break;

default: //LastName
query.Where(c =SqlMethods.Like(c.Person.LastName, "%" +
txtSearchCriteria.Text + "%"));
break;
}

employees = query.ToList();

BindGrid(employees);
"mesut" wrote:
Hi Mike, there are several ways of doing this. But I got this way
working...

I hope it helps,

cheers, mesut
objDataContext = DatabaseFactory.GetLinq();
List<Linq.tblCostLocalTable;
var query = (from costing in objDataContext.tblCosts
select costing);

//the where code below (2 lines) are
replace by dynamic where
//where costing.ProductID == productID &&
//costing.BusinessUnit == businessUnit

if (productID != 0) query = query.Where(cost =>
cost.ProductID == productID);
if (!string.IsNullOrEmpty(businessUnit)) query =
query.Where(cost =cost.BusinessUnit == businessUnit);

LocalTable = query.ToList();

return LocalTable;
Sep 19 '08 #3
the where clause in a Linq query does not set a property, its a method
(actually an extension method) that returns another IQueryable object that
the Select method (which also return an IQueryable) can be called on. your
code is throwing away the results of the Where.


-- bruce (sqlwork.com)
"Mike Collins" wrote:
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.UserProfileemplo yees =
(IQueryable<BusinessLogic.Services.UserProfile>)(L ist<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.UserProfileemployees = 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.UserProfilepage dResult =
SearchEmployees(filter);
employees =
pagedResult.Results.ToList<BusinessLogic.Services. UserProfile>();
BindGrid(employees.ToList<BusinessLogic.Services.U serProfile>());
}
}
catch (ThreadAbortException) { }
catch (Exception ex) { ExceptionHelper.Publish(ex); }
}
Sep 19 '08 #4
So, it seems that I do not need to cast my Session object to an IQueryable
object then...since that line does not work anyway. I can just cast it to a
list<>, then put the results of the where into an IQueryable object at that
time. Is that correct?

"bruce barker" wrote:
the where clause in a Linq query does not set a property, its a method
(actually an extension method) that returns another IQueryable object that
the Select method (which also return an IQueryable) can be called on. your
code is throwing away the results of the Where.


-- bruce (sqlwork.com)
"Mike Collins" wrote:
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.UserProfileemplo yees =
(IQueryable<BusinessLogic.Services.UserProfile>)(L ist<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.UserProfileemployees = 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.UserProfilepage dResult =
SearchEmployees(filter);
employees =
pagedResult.Results.ToList<BusinessLogic.Services. UserProfile>();
BindGrid(employees.ToList<BusinessLogic.Services.U serProfile>());
}
}
catch (ThreadAbortException) { }
catch (Exception ex) { ExceptionHelper.Publish(ex); }
}
Sep 19 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: topmind | last post by:
I am generally new to dot.net, coming from "scriptish" web languages such as ColdFusion and Php. I have a few questions if you don't mind. First, how does one go about inserting dynamic SQL...
7
by: Ronald S. Cook | last post by:
I've always been taught that stored procedures are better than writing SQL in client code for a number of reasons: - runs faster as is compiled and lives on the database server - is the more...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
15
by: EDBrian | last post by:
My problem is this. Our clients create different fields they want to collect and we allow them build dynamic filters, reports etc... We run some TSQL to actually create the column and all works...
1
by: john | last post by:
I'm trying to build a LINQ expression that will use a dynamic construction of a LIKE statement in the WHERE clause, it would look something like this in SQL: WHERE TaskGroup Like "*00*" OR...
2
by: Joey | last post by:
I am querying a DataSet with LINQ. I am running into a problem when trying to construct my query because in the "from" clause I do not know the table name (range variable) until runtime. Possible...
4
by: Jon Skeet [C# MVP] | last post by:
On Jul 15, 4:01pm, raulavi <raul...@discussions.microsoft.comwrote: Are you talking about LINQ to Objects or LINQ to SQL? For the latter, I'd say it contains two implicit joins - one in the fact...
2
by: =?Utf-8?B?Tmljaw==?= | last post by:
Hello, I need some assistance with a LINQ query. I've got a simple query: var q = from t in db.Table1 select t; Based on user input I'm adding some where clauses: if (condition1) q =...
0
by: Jay Douglas | last post by:
Hello, I've found some posts on creating dynamic WHERE clauses in LINQ to SQL using predicate builders and extending lamda expressions. One issue with these posts is all the examples only use a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.