0

I have a column in dataTable with blank rows

Column1
A
B
C

D

E

I need to set if exist and to avoid adding, but blank rows should not be counted. Only rows with data should be in the (if exists). Thanks

 bool exists = dt.Select().ToList().Exists(row => row["column1"].ToString() == txtbox)



if(exists == true)
{}
else

// it includes blank so it goes to true, which I need is blank rows should not be included.

Alphatrix
  • 83
  • 8
  • 1
    How can a blank row be included if you search for "search field"? Just handle the case that the user didn't enter anything into the search TextBox. – Tim Schmelter Sep 12 '18 at 08:10
  • that search field is a textbox that can be empty or whitespaces. So first I need to collect all the rows withour blank then in that collection I will check if exist based on the search fields. But I don't know the code – Alphatrix Sep 12 '18 at 08:12
  • Possible duplicate of [Avoiding duplicate code in Linq Select method](https://stackoverflow.com/questions/34649878/avoiding-duplicate-code-in-linq-select-method) – Pvria Ansari Sep 12 '18 at 08:12
  • Can you provide a meaningful example? _"search field can be empty or whitespaces"_ .. _"...collect all the rows without blanks"_ .. _"I will check if exist based on the search field"_ Doesn't make sense. – Tim Schmelter Sep 12 '18 at 08:13
  • bool exists = dt.Select().ToList().Exists(row => row["column1"].ToString() == txtbox), I already had this code but it includes blank string "". I need to bypass that "" blank string. – Alphatrix Sep 12 '18 at 08:14
  • @Alphatrix: edit your question and provide a meaningful sample with input, data and expected result. – Tim Schmelter Sep 12 '18 at 08:16
  • I don't understand why can't you add the condition in the `Exists` lambda? Also, why does it return false? why would the extra rows bother if you use `Exists`? it should return true. – Ofir Winegarten Sep 12 '18 at 08:23
  • I need to return false to output Messagebox. – Alphatrix Sep 12 '18 at 08:26
  • So, if the only thing missing is check for empty string of the txtbox, then add it, instead of trying to reduce rows. Which you can do either. – Ofir Winegarten Sep 12 '18 at 08:27

5 Answers5

1
var lignesNonContainEmptyString = dt.Select()
                   .Where(row => row["column1"] != null 
                                && row["column1"].ToString() == txtbox 
                                && !string.IsNullOrEmpty(row["column1"].ToString()))

bool exists = lignes.Count() != 0;

OR

bool exists = dt.Select()
                       .Any(row => row["column1"] != null 
                                    && row["column1"].ToString() == txtbox 
                                    && !string.IsNullOrEmpty(row["column1"].ToString()))
Antoine V
  • 6,998
  • 2
  • 11
  • 34
  • bool exists = dt.Select().ToList().Exists(row => row["column1"].ToString() == txtbox), this code already work but it does not support blank rows, I need to bypass those blank rows, and it should return true or false – Alphatrix Sep 12 '18 at 08:24
  • Wow, thanks it really worked and saved a lot of time. – Alphatrix Sep 12 '18 at 08:38
  • Instead of `lignes.Count() != 0`, I like `Lines.Any()`. Readable and more efficient. – Drag and Drop Sep 12 '18 at 08:39
  • 1
    `dt.Select().ToList()` please not! `DataTable` is an in-memory collection that can be queried with LINQ(`AsEnumerable()`). `DataTable.Select` will iterate all rows and create a new `DataRow[]`. `ToList()` will iterate all rows and create a `List(Of DataRow)`. Instead of `Exists` you can use `Any`. – Tim Schmelter Sep 12 '18 at 08:42
  • @TimSchmelter, I see a bug in the matrix. Same comment same author :) – Drag and Drop Sep 17 '18 at 15:00
1

You would like to return false if the textbox is empty, so add the condition for the textbox.

bool exists = !string.IsNullOrWhiteSpace(txtbox)
              && dt.Select().ToList()
                   .Exists(row => row["column1"].ToString() == txtbox)

By the way, instead of using .Select().ToList(), you can add a reference to System.Data.DataTableExtensions and use the Extension AsEnumerable:

dt.AsEnumerable().Any( .....
Ofir Winegarten
  • 9,215
  • 2
  • 21
  • 27
0

this code can help you

from a in list where ( Column1 != null || Column1 != "")
&& Column1 == searchfield
select a
Mahyar Mottaghi Zadeh
  • 1,178
  • 6
  • 18
  • 31
Reza Faghani
  • 141
  • 1
  • 10
  • bool exists = dt.Select().ToList().Exists(row => row["column1"].ToString() == txtbox). I need to modify this code, becuase it includes the blank input – Alphatrix Sep 12 '18 at 08:15
0

How about this: non linq way

bool DataTableNonEmptyCount()
{
int count =0;
foreach (DataColumn col in row.Table.Columns)
    if (!row.IsNull(col))
      count ++;

  return count;
}

This will return a count of all non null row in column.

Gauravsa
  • 6,330
  • 2
  • 21
  • 30
0

Based on Thierry's answer, you can also use the Any(predicate) syntax:

var existsLineWithoutEmptyString = 
             dt.AsEnumerable()
               .Any(row => row["column1"] != null 
                           && row["column1"].ToString() == txtbox 
                           && !string.IsNullOrEmpty(row["column1"].ToString()))
Drag and Drop
  • 2,672
  • 3
  • 25
  • 37