Data Platform Developer Center >
Data Platform Development Forums
>
LINQ to SQL
>
Dynamic Filtering in Linq to SQL with GridView
Dynamic Filtering in Linq to SQL with GridView
- 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
- 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- Marked As Answer byYichun_FengMSFT, ModeratorFriday, November 13, 2009 4:17 AM
- 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- Marked As Answer byYichun_FengMSFT, ModeratorFriday, November 13, 2009 4:17 AM
- 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
- 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 . - 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. - 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- Marked As Answer byYichun_FengMSFT, ModeratorFriday, November 13, 2009 4:17 AM
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.- 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- Marked As Answer byYichun_FengMSFT, ModeratorFriday, November 13, 2009 4:17 AM
- Hi David,
The Selecting event will only fire if I issue aGridView.DataBind();
- 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


