0

I have a DataTable which I want to query one column at a time to check if it meets the set criteria, for some reason the below code gives correct results for the first column after that the results are incorrect.

private bool BusinessRulesOne(DataTable dt, DataColumn dc)
{
    bool isSatisfied = false;
    DataRow[] checkColumn = dt.Select(dc.ColumnName + " " + "in (1,2,3,)");

    if (checkColumn.Length != 0)
    {
        isSatisfied = true;
    }
    return isSatisfied;
}

On this method Im passing the DataTable I'm querying and DataColumn that I'm currently concentrating on. What needs to happen here is that I want to check if the values on this column consists of the values 1,2 and 3. If yes then return true.

private bool BusinessRulesTwo(DataTable dt, DataColumn dc)
{
    bool isSatisfied = false;
    var checkColumn = dt.Select(dc.ColumnName + " " + " = 1");

    if (checkColumn.Count() > 3)
    {
        isSatisfied = true;
    }
    return isSatisfied;
}

On the other rule I'm checking if the number of one's (1's) in that column are more than three(3) if yes return true.

Any suggestions are welcome, I'm not clued up with Linq but willing to learn and explore it if it makes life easier.

Edit With Values in One column enter image description here

Lord-David
  • 535
  • 1
  • 11
  • 34
  • please explain better what issue exactly you have with this code. does it produce incorrect output? do you want to combine two methods in one? do you want to rewrite methods with Linq? I don't see a question mark in the post. – ASh Mar 20 '17 at 06:53
  • Sorry for the Unclearity. The code above produces incorrect output, Its suppose to query a DataColumn in a DataTable. On the first rule its suppose to return true if a DataColumn consists of numbers 1,2 and 3. It does that correctly with the first Column. On the second column it returns false instead of true. Hence I'm looking for suggestions, If there is a way in Linq that will be fine too. – Lord-David Mar 20 '17 at 07:02

2 Answers2

2

At first you should know that data table doesn't implement IEnumerable so to use Linq here you should use AsEnumerable: For the first rule:

private bool BusinessRulesOne(DataTable dt, DataColumn dc)   { return dt.AsEnumerable().Any(dataRow => !String.IsNullOrEmpty(dataRow.Field<string>(dc.ColumnName).First().ToString()) && ( dataRow.Field<int>(dc.ColumnName)  == 1  || dataRow.Field<int>(dc.ColumnName)  == 2 || dataRow.Field<int>(dc.ColumnName)  == 3));}

For the second rule:

private bool BusinessRulesTwo(DataTable dt, DataColumn dc) { return  dt.AsEnumerable().Where(dataRow => !String.IsNullOrEmpty(dataRow.Field<string>(dc.ColumnName).First().ToString()) && ( dataRow.Field<int>(dc.ColumnName) == 1)).Count() > 3 ;}
Abdullah Dibas
  • 1,499
  • 1
  • 9
  • 13
2

"if the values on this column consists of the values 1,2 and 3, then return true"

BusinessRulesOne doesn't express this condition properly. It works differently: "if column contains at least one value from set, then return true". It doesn't guarantee what there are no other values.

here is modified method with inverted logic: "if this column contains smth except 1,2 or 3, then return false"

private bool BusinessRulesOne(DataTable dt, DataColumn dc)
{
    DataRow[] checkColumn = dt.Select(dc.ColumnName + " not in (1,2,3)");

    return checkColumn.Length == 0;
}
ASh
  • 34,632
  • 9
  • 60
  • 82
  • I checked this it solves the `BusinessRulesOne` method, it seems to be returning correct data. Any idea on the second one? – Lord-David Mar 20 '17 at 07:41
  • @Lord-David, expression looks correct, it should work. can you give an example of DataTable values which produce wrong result? – ASh Mar 20 '17 at 07:46
  • See the question edit, thats a column with data. on that I'm saying if 1's are more than three return true. But it doesn't as you can see on that column the 1's are more than three. – Lord-David Mar 20 '17 at 07:52
  • @Lord-David, set a break point on line `if (checkColumn.Count() > 3)` and when it hit, check which rows are in `checkColumn` array. – ASh Mar 20 '17 at 07:59
  • I did, its True for the first column after that its False which shouldn't be the case. – Lord-David Mar 20 '17 at 08:33