jongrossex
asked on
Linq Data Source - adding a where clause
Newby question related to LINQ and LINQ DataSource.
I have a standard ASP web page with a gridview attached to a linq data source. A sort order is applied to the data so the most important data is generally at the top but sometimes I need to search (a very simple search)...
I have the code working by putting a hard coded string into .Where but I am confident that is NOT the best way to do this.
With User_Grid_LinqDataSource
.Where = "First_Name.Contains(@Sear chText) or Last_Name.Contains(@Search Text)"
.WhereParameters.Clear()
.WhereParameters.Add("Sear chText", txtSearch.Text.Trim)
End With
The rest of the story... I have 2 distinct searches based on a combo-box. 'Name' is shown above and 'Address' is the other one.
How do I use linq to apply different where clauses?
I have a standard ASP web page with a gridview attached to a linq data source. A sort order is applied to the data so the most important data is generally at the top but sometimes I need to search (a very simple search)...
I have the code working by putting a hard coded string into .Where but I am confident that is NOT the best way to do this.
With User_Grid_LinqDataSource
.Where = "First_Name.Contains(@Sear
.WhereParameters.Clear()
.WhereParameters.Add("Sear
End With
The rest of the story... I have 2 distinct searches based on a combo-box. 'Name' is shown above and 'Address' is the other one.
How do I use linq to apply different where clauses?
ASKER
Thanks. My questions was NOT clear, my fault.
I understand how to create the LINQ query but I do not understand how to associate it back with the LinkDataSource so that it is reflected in the grid.
So after I do the query like below...
var queryLondonCustomers = from cust in customers
where cust.City == "London"
select cust;
How do I associate the results with the data source so that it will be displayed on the grid. I assume it is simple but I can not find the answer... or is there a better way to think about this.
-Jon
Try using the Where property of the LinqDataSource itself to do the filtering
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.where.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.where.aspx
ASKER
We are getting closer... but from the above link I do not want my query coded in a string.
The following code attaches linq data to a gridview
Dim db As New SjuDatalassesDataContext
Dim searchText As String = txtSearch.Text.Trim
Dim userList = From u In db.vUser Where u.First_Name.Contains(sear chText) Select u
grid.DataSourceID = Nothing
grid.DataSource = userList
grid.DataBind()
How do I either
- tell the LinqDataSource 'control' to execute the query from the above Linq command
OR
- attached userlist to the LinqDataSource 'control'
The following code attaches linq data to a gridview
Dim db As New SjuDatalassesDataContext
Dim searchText As String = txtSearch.Text.Trim
Dim userList = From u In db.vUser Where u.First_Name.Contains(sear
grid.DataSourceID = Nothing
grid.DataSource = userList
grid.DataBind()
How do I either
- tell the LinqDataSource 'control' to execute the query from the above Linq command
OR
- attached userlist to the LinqDataSource 'control'
hi
can you give a try to the select property of the linqdatasource control http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.select.aspx
Linqdatasource control is meant to perform the linq operations..... hence it comes with all required properties and methods to perform linq operations... I am not sure why you want to deviate from this...
can you give a try to the select property of the linqdatasource control http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.select.aspx
Linqdatasource control is meant to perform the linq operations..... hence it comes with all required properties and methods to perform linq operations... I am not sure why you want to deviate from this...
ASKER
What I am trying to get away from is a string for the where clause
.Where = "First_Name.Contains(@Sear
If my data model changes I do not find out till runtime
Does the 'linqdatasource' have a property that allows me to add a link command? For example:
From u In db.vUser Where u.First_Name.Contains(sear
I have looked and did not see any such functionality.
Thanks for your help
no you can't do that...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That seems exactly what I was looking for. I will give it a try tomorrow.
Always enjoy Scott Gu's blogs.
Always enjoy Scott Gu's blogs.
Any Luck?
ASKER
Sorry for the delay... that was perfect.
pl refer here for some quick samples: http://msdn.microsoft.com/en-us/library/bb397927.aspx