RowFilter with IN operator over a column of type Guid


RowFilter with IN operator over a column of type Guid

Case

Apply a RowFilter on a DataView which filters a column of type System.Guid and can contain a variable number of values.

Example:

DataView dv = new DataView();

dv.RowFilter = \”TypeId in (\’\’, \’\’, …)\”;

where TypeId column is of a System.Guid type. As the number of values is variable it makes perfect sense to use the in operator.

The problem

When you are looking at this expression you may think this should be working as expected, i.e. the result should only contain the data where TypeId is in the specified values. In fact, if you run it you will get the following exception:

Cannot perform \’=\’ operation on System.Guid and System.String.

The Solution

To convert the String to Guid you should use the Convert(expression, type) method like this:

dv.RowFilter = \”TypeId in (Convert(\’\’, \’System.Guid\’), Convert(\’\’, \’System.Guid\’),

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s