ASP.Net

Dynamic load where parameters for linqDatasource

Google+ Pinterest LinkedIn Tumblr

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

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

  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