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.

   Parameter whereparam = new Parameter();
   whereparam.Name = “BlogId”;
   whereparam.DefaultValue = BlogManager.CurrentBlogId.ToString();
   whereparam.Type = TypeCode.Object;
   linqDataSource.Where = “BlogId == Guid(@BlogId)“;

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’


Senior Technical Evangelist with the Technical Marketing team at Sitecore. Working closely with product development and product management to help customers, partners and the developers unlock the full potential of Sitecore.


  1. 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.

  2. Another option instead of doing it in the code behind:

    Just update the where line for the LinqDatasource in design.
    Where=”BlogID == Guid(@BlogID)”>

  3. Brian Holmes Reply

    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

Write A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.