Data Platform Developer Center > Data Platform Development Forums > LINQ to SQL > Dynamic Filtering in Linq to SQL with GridView
Ask a questionAsk a question
 

AnswerDynamic Filtering in Linq to SQL with GridView

  • Sunday, November 08, 2009 4:34 AMChui Kean Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Dear All,

    In mDataSource.Load(), I have added code below:

    mDataSource.Where = "(Privilege & @Priv) > 0";
    mDataSource.WhereParameters.Add("Priv", TypeCode.Int32, privilege.ToString());

    Where, Privilege is an INT type. Unfortunately I kept getting this error:

        Operator '>' incompatible with operand types 'String' and 'Int32'

    I really have no clue how to fix this problem, since everything are of Int32 type.

    Thanks in advance.

Answers

  • Monday, November 09, 2009 4:57 PMDavid DeWinter - MSFTAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Chui,

    If you use a custom LINQ query expression within the Selecting event handler and set e.Result equal to that expression, then it should be translated appropriately. See ScottGu's blog post for an example of this. (See the section "Using the <asp:LinqDataSource> Selecting Event.)

    Even though Scott doesn't show it, you can get parameters from the WhereParameters property of the LinqDataSourceSelectEventArgs.

    Hope this helps,
    David
    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
  • Wednesday, November 11, 2009 9:00 PMDavid DeWinter - MSFTAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    As far as I can tell, the Selecting event will be fired every time. Are you seeing cases where it would not be?

    After some investigation I found that the syntax for the LinqDataSource is based on the sample for .NET for dynamic LINQ queries, which ScottGu blogged about here . Based on the documentation with that sample, "&" is interpreted as string concatenation, as you mentioned. I did not find anything about bitwise operators.
    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
  • Saturday, November 14, 2009 9:44 PMDavid DeWinter - MSFTAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Yes, that is by design. The GridView won't attempt to use the data you've set as the data source until you call DataBind().
    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
    • Marked As Answer byChui Kean Sunday, November 15, 2009 7:30 AM
    •  

All Replies

  • Monday, November 09, 2009 2:48 AMDavid DeWinter - MSFTAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    As I mentioned in your other thread, I'm not sure whether this is supported. A workaround is to use the Selecting event on the LinqDataSource to specify exactly what you want through LINQ directly.

    Someone on the ASP.NET team may be able to help if you ask here .
  • Monday, November 09, 2009 3:07 AMChui Kean Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    David,

    Thanks for the answer.

    I had actually tried adding the filter in the Selecting event as well.

    Unfortunately, LinqDataSource doesn't know how to translate the parameters to Int32 when we where using Bitwise AND operation.
  • Monday, November 09, 2009 4:57 PMDavid DeWinter - MSFTAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Chui,

    If you use a custom LINQ query expression within the Selecting event handler and set e.Result equal to that expression, then it should be translated appropriately. See ScottGu's blog post for an example of this. (See the section "Using the <asp:LinqDataSource> Selecting Event.)

    Even though Scott doesn't show it, you can get parameters from the WhereParameters property of the LinqDataSourceSelectEventArgs.

    Hope this helps,
    David
    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
  • Tuesday, November 10, 2009 12:29 PMChui Kean Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    David,

    Thanks for the hint.

    Ok, that could be the fix, but unfortunately I don't know how to make sure this Selecting event trigger every time (or whenever needed) during postback. Do you have any clue?


    BTW, I think I know what caused this BUG.

    LinqDataSource wrongly interpreted the '&' operator as the string concatenation sign in VB.

    But at the same time correctly interpreted the ‘AND’ operator as logical AND.

    So, question now is, is there any other way to represent a bitwise AND operator?

    Thanks in advance.

  • Wednesday, November 11, 2009 9:00 PMDavid DeWinter - MSFTAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    As far as I can tell, the Selecting event will be fired every time. Are you seeing cases where it would not be?

    After some investigation I found that the syntax for the LinqDataSource is based on the sample for .NET for dynamic LINQ queries, which ScottGu blogged about here . Based on the documentation with that sample, "&" is interpreted as string concatenation, as you mentioned. I did not find anything about bitwise operators.
    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
  • Saturday, November 14, 2009 8:14 AMChui Kean Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi David,

    The Selecting event will only fire if I issue a

    GridView.DataBind();

  • Saturday, November 14, 2009 9:44 PMDavid DeWinter - MSFTAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Yes, that is by design. The GridView won't attempt to use the data you've set as the data source until you call DataBind().
    Blog - http://blogs.rev-net.com/ddewinter/ Twitter - @ddewinter
    • Marked As Answer byChui Kean Sunday, November 15, 2009 7:30 AM
    •