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

pitchoune said:

Thanks very much! It helps.

# October 29, 2009 10:22 AM

Glade Mellor said:

Thanks! You saved me a serious late night coding headache!

# October 5, 2010 12:21 AM

Glenn Quagmire said:

I had this EXACT problem and this gave me the EXACT answer without a lot of BS.

Thanks!  Giggity~

# December 10, 2010 12:04 AM

ArvinBoggs said:

Thanx for sharing your tip. Saved me minutes of coding.

# April 28, 2011 12:36 AM

Burberry outlet store said:

It is obviously that selling informative products online is perfectly fit to the affiliate marketing business.

# July 20, 2011 9:53 PM