views:

548

answers:

1

I'm having problems with a DataTable line, the idea is that I want to pull out of a series of rows any rows where one of the column values is in a set of guids. However, I'm getting a guid / string comparison error from the select() function.

The code is as follows:

Dim sb As New StringBuilder
For Each Row As DataRow In dtData.Rows
    sb.Append("'")
    sb.Append(Row("SomeField").ToString)
    sb.Append("', ")
Next


gvDataView.DataSource = dtSubData.Select("SomeField IN (" & sb.ToString.TrimEnd(", ".ToCharArray) & ")")

However, as SomeField is a uniqueidentifier column, dtSubData.select is throwing an error about Guid and String Comparison. With this in mind, is there any way I can work around this issue?

A: 

If you can use .NET 3.5 and System.Data.DataSetExtensions.dll, you might be able to take advantage of LINQ here. First, I'd populate a HashSet with the values from dtData.

var items = from row in dtData.AsEnumerable()
            select row.Field<Guid>("SomeField");
var validValues = new HashSet<Guid>(items);

Apologies for the C#, but my VB LINQ syntax is a bit shaky. Anyway, once you have a HashSet of the valid id's, you can efficiently test whether data from your second DataTable is contained in that set:

gvDataView.DataSource = from row in dtSubData.AsEnumerable()
                        where validValues.Contains(row.Field<Guid>("SomeField"))
                        select row;
Joel Mueller