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 = "tableId in ('<guid>', '<guid>', ...)";
where tableId column is of a System.Guid type. As the number of values is variable it makes perfect sense to use the in operator.
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 tableId 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.
Solution :
To convert the String to Guid you should use the Convert(expression, type) method like this:
dv.RowFilter = "tableId in (Convert('<guid>', 'System.Guid'), Convert('<guid>', 'System.Guid'))";
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 = "tableId in ('<guid>', '<guid>', ...)";
where tableId column is of a System.Guid type. As the number of values is variable it makes perfect sense to use the in operator.
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 tableId 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.
Solution :
To convert the String to Guid you should use the Convert(expression, type) method like this:
dv.RowFilter = "tableId in (Convert('<guid>', 'System.Guid'), Convert('<guid>', 'System.Guid'))";
could we not convert tableId to string instead? so we'd have one conversion instead of as many as there are values in the IN clause? → yes, we absolutely can, it is shorter and faster.
ReplyDeletedataTable.DefaultView.RowFilter = $"Convert(tableId, 'System.String') IN ('{ String.Join("','", listOfGuids) }')";