DataTable.Select() - Comparing GUIDs
I've a datatable with a column containing GUIDs. I want to select a row matching a specific GUID. I wrote the following cod开发者_StackOverflowe,
DataRow[] dRows = dtListSettings.Select("ListGUID = " + Convert.ToString(ViewState["GUID"]));
The GUID i'm comparing is 500c2b6a-a3a7-457f-90ed-c96768d91520. But i'm getting the error - Syntax error: Missing operand after 'c2b6a' operator.
Any ideas?
Thank you NLV
Need a single quote:
Something like:
string.Format("ListGUID = '{0}'", Convert.ToString(ViewState["GUID"]));
Try surrounding your select statement parameter in single quotes, like this:
DataRow[] dRows = dtListSettings.Select("ListGUID = '" + Convert.ToString(ViewState["GUID"]) + "'");
This does not work for IN. E.g.
DataRow[] dRows = dtListSettings.Select("ListGUID IN ('" + Convert.ToString(ViewState["GUID"]) + "')");
I resolve this with two bellow methods:
DataTable dt = new DataTable(); // your datatable with data
Guid gid = Guid.NewGuid(); // searching guid
DataRow[] dra = dt.Select("GuidColumn = '" + gid.ToString() + "'")
// OR
DataRow[] dra = dt.Select("GuidColumn = Convert('" + gid.ToString() + "', 'System.Guid')");
hope be useful ;)
精彩评论