21
2008
Dynamic load where parameters for linqDatasource
When using an Linq Datasource (linq to sql) for a datagrid (Gridview, ListView, etc) you don’t always want all rows from a table. To exclute data you can use the WhereParameters to add an where statement to your DataSource.
You can do limited where statements with the Visual Studio Wizards. When you need more than a limited statement you can dynamicly create an Parameter to add to your Linq DataSource.
The following examples shows how to filter the Linq Datasource that recovers all rows from the Post table and filters them on the BlogId. The BlogManager.CurrentBlogId gets the blog GUID from the Session.
[code:c#]
Parameter whereparam = new Parameter();
whereparam.Name = "BlogId";
whereparam.DefaultValue = BlogManager.CurrentBlogId.ToString();
whereparam.Type = TypeCode.Object;
linqDataSource.WhereParameters.Add(whereparam);
linqDataSource.Where = "BlogId == Guid(@BlogId)";
[/code]
You convert the @BlogId (string) to a Guid In the linqDataSource.Where property otherwise you get the following error:
Operator ‘==’ incompatible with operand types ‘Guid’ and ‘String’
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.Query.Dynamic.ParseException: Operator ‘==’ incompatible with operand types ‘Guid’ and ‘String’
5 Comments + Add Comment
Leave a comment
Who am I
Sitecore blogs
Sitecore blogs
- Sitecore Descriptive Tree List Field Type
- Adding a Publish button in the contextal menu
- Sitecore Searcher and Advanced Database Crawler
- Dutch webinar about faster developing with Sitecore Rocks and VS2010
- Using XAML controls outside the Sitecore folder
- Easily Sanitize a Sitecore Item Name in C#
- Introduction to the Unified Page Editor
- Sitecore.Diagnostics.Assert statements
- Programmatically Add Controls to the HTML Head in Sitecore
- Filtering ECM Dispatcher

An article by Mark





This helped me a lot! Awesome!
I was just playing around with this to get the windows authenticated user and filtering a grid display for entries by that user. I was trying to figure out the asp, but I’m not that great with asp. The next thought was to save it in the session and do that, but I happened across this and wahoo! Thank you.
I like C#. Works great!
Next thing I’ll need to try is to have a dropdown list and let the user select a different LoginName. For those people that have seen their stuff, now they want to look at somebody elses. So, I’ll need to find that where param and change the default value.
whereparam.DefaultValue = new value
and probably databind.
thanks… good stuff.
john.
Another option instead of doing it in the code behind:
Just update the where line for the LinqDatasource in design.
Where=”BlogID == Guid(@BlogID)”>
Thanks for your post. Helped me get to a solution. Here’s my post.
http://usman-suglatwala.blogspot.com/2011/04/generating-dynamic-querywhere-clause.html
Hi. This post has been very helpful in a search portion of a page I am working on (needs a dynamic search of 11 controls). I have my search working so far (2 down…9 to go?!) but I have paging enabled (using it to load in a GridView control) and when you click on the Column Headers it goes to the default design time set of records (not good) rather than persisting my where and whereparameter properties I set in my code behind. Anyone know how to persist these? Is there simply a method on the LinqDataSource I need to call?…again great post!
- Brian