Solved

Linq Data Source - adding a where clause

Posted on 2011-09-12
11
313 Views
Last Modified: 2012-05-12
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(@SearchText) or Last_Name.Contains(@SearchText)"
   .WhereParameters.Clear()
   .WhereParameters.Add("SearchText", 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?

0
Comment
Question by:jongrossex
  • 5
  • 3
  • 3
11 Comments
 
LVL 9

Expert Comment

by:dexterrajesh
ID: 36524595
hi,

pl refer here for some quick samples: http://msdn.microsoft.com/en-us/library/bb397927.aspx
0
 

Author Comment

by:jongrossex
ID: 36524813

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

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36526202
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
0
 

Author Comment

by:jongrossex
ID: 36527153
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(searchText) 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'


0
 
LVL 9

Expert Comment

by:dexterrajesh
ID: 36527809
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...
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:jongrossex
ID: 36529238

What I am trying to get away from is a string for the where clause
   .Where = "First_Name.Contains(@SearchText) or Last_Name.Contains(@SearchText)"

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(searchText) Select u
I have looked and did not see any such functionality.

Thanks for your help
0
 
LVL 9

Expert Comment

by:dexterrajesh
ID: 36530384
no you can't do that...
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 36531946
0
 

Author Comment

by:jongrossex
ID: 36533903
That seems exactly what I was looking for.  I will give it a try tomorrow.

Always enjoy Scott Gu's blogs.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36570193
Any Luck?
0
 

Author Comment

by:jongrossex
ID: 36570316
Sorry for the delay... that was perfect.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now