I am trying to search the datatable for non-unique values and I keep getting this error
Filter expression ‘PROV_NEW’ does not evaluate to a Boolean term.
when I run it.
This is my code:
public class GetData
{
datalogiclayer.TableSetup dal;
DataSet ds;
public delegate void InvalidTableDataDelegate(string ErrorMessage);
public event InvalidTableDataDelegate InvalidTableData;
public delegate void SetupDataLoadedDelegate(System.Data.DataSet dv, string TableName);
public event SetupDataLoadedDelegate SetupDataLoaded;
public GetData()
{
dal = new datalogiclayer.TableSetup();
}
public void Update(DataSets.Setup ds)
{
try
{
string errMsg = string.Empty;
if (ValidateTable(ds.SETUP_MWPROV, out errMsg))
{
dal.UpdateDatabase(ds);
}
else
{
if (InvalidTableData != null)
InvalidTableData(errMsg);
}
}
catch (Exception)
{
throw;
}
}
private bool ValidateTable(DataSets.Setup.SETUP_MWPROVDataTable dt, out string TableIssues)
{
try
{
//NewCode not used for other row
DataRow[] result = dt.Select("PROV_NEW = ''");
DataRow[] dupresults = dt.Select("PROV_NEW");
TableIssues = string.Empty;
DataTable dtTemp = dt.DefaultView.ToTable(true, "NEW_PROV");
if (dupresults.Length == 0)
{
return true;
}
else
{
IEnumerable<DataRow> uniqueCodes = dupresults.AsEnumerable().Distinct(DataRowComparer.Default);
Console.WriteLine("Unique Provider Codes:");
foreach (DataRow NEW_PROV in uniqueCodes)
{
Console.WriteLine(NEW_PROV.Field<Int32>("PROV_NEW"));
}
return false;
}
}
catch (Exception)
{
throw;
}
}
}
So as you can see I have it searching for unique rows in the datatable, when the exception is being thrown is when I actually go to save the data. And it gives the error that is up above.
The argument for
dt.Selectis a filter."PROV_NEW"is not a valid filter as the expression doesn’t result to a boolean value. I believe you’re confusing this with SQL, where you can ask it to select a single column. But you don’t need to do that because later you need to select distinct off of the PROV_NEW column. If you want to make sure the whole row is distinct, then this is all you need to do.If you only want compare the PROV_NEW column, you could write a custom equality comparer that looks at only your PROV_NEW column. You can reference your custom comparer when you call
distinctwith >this version <.Alternatively Grant Winney suggested you could use Linq to select only the single column before using distinct. Refer to comments in their answer.