Emil Stoichev's Blog

Just a few coding tips

Tip: 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 ('<guid>', '<guid>', ...)";

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('<guid>', 'System.Guid'), Convert('<guid>', 'System.Guid'), ...)";

Comments

Frank Quednau said:

Wow! I searched for "Rowfilter compare guid", found your page, and it solves exactly my problem. I love the Internets! Cheers...

# November 5, 2008 11:16 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)