Check If Value Exists in Datatable

Check if value exists in dataTable?

You can use LINQ-to-DataSet with Enumerable.Any:

String author = "John Grisham";
bool contains = tbl.AsEnumerable().Any(row => author == row.Field<String>("Author"));

Another approach is to use DataTable.Select:

DataRow[] foundAuthors = tbl.Select("Author = '" + searchAuthor + "'");
if(foundAuthors.Length != 0)
{
// do something...
}

Q: what if we do not know the columns Headers and we want to find if any
cell value PEPSI exist in any rows'c columns? I can loop it all to
find out but is there a better way? –

Yes, you can use this query:

DataColumn[] columns = tbl.Columns.Cast<DataColumn>().ToArray();
bool anyFieldContainsPepsi = tbl.AsEnumerable()
.Any(row => columns.Any(col => row[col].ToString() == "PEPSI"));

How to check if value exist in datatable

Try using {search: 'applied'} selector-modifier

var table = $('#example').DataTable()
console.log(table.search('Tokyo').row({search: 'applied'}).data())
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script><link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet" />

<table id="example" class="display" style="width:100%"> <thead> <tr> <th>Name</th> <th>Position</th> <th>Office</th> <th>Numero</th> <th>Start date</th> <th>Salary</th> </tr> </thead> <tbody> <tr> <td>Tiger Nixon</td> <td>System Architect</td> <td>Edinburgh</td> <td>155555</td> <td>2011/04/25</td> <td>$320,800</td> </tr> <tr> <td>Garrett Winters</td> <td>Accountant</td> <td>Tokyo</td> <td>63</td> <td>2011/07/25</td> <td>$170,750</td> </tr> <tr> <td>Ashton Cox</td> <td>Junior Technical Author</td> <td>San Francisco</td> <td>1</td> <td>2009/01/12</td> <td>$86,000</td> </tr> </tbody></table>

Check if String / Record exists in DataTable

Something like this

 string find = "item_manuf_id = 'some value'";
DataRow[] foundRows = table.Select(find);

Simplest/fastest way to check if value exists in DataTable in VB.net?

If the data in your DataTable doesn't change very often, and you search the DataTable multiple times, and your DataTable contains many rows, then it's likely going to be a lot faster to build your own index for the data.

The simplest way to do this is to sort the data by the key column so that you can then do a binary search on the sorted list. For instance, you can build an index like this:

Private Function BuildIndex(table As DataTable, keyColumnIndex As Integer) As List(Of String)
Dim index As New List(Of String)(table.Rows.Count)
For Each row As DataRow in table.Rows
index.Add(row(keyColumnIndex))
Next
index.Sort()
Return index
End Function

Then, you can check if a value exists in the index quickly with a binary search, like this:

Private Function ItemExists(index As List(Of String), key As String) As Boolean
Dim index As Integer = index.BinarySearch(key)
If index >= 0 Then
Return True
Else
Return False
End If
End Function

You could also do the same thing with a simple string array. Or, you could use a Dictionary object (which is an implementation of a hash table) to build a hash index of your DataTable, for instance:

Private Function BuildIndex(table As DataTable, keyColumnIndex As Integer) As Dictionary(Of String, DataRow)
Dim index As New Dictionary(Of String, DataRow)(table.Rows.Count)
For Each row As DataRow in table.Rows
index(row(keyColumnIndex)) = row
Next
Return index
End Function

Then, you can get the matching DataRow for a given key, like this:

Dim index As Dictionary(Of String, DataRow) = BuildIndex(myDataTable, myKeyColumnIndex)
Dim row As DataRow = Nothing
If index.TryGetValue(myKey, row) Then
' row was found, can now use row variable to access all the data in that row
Else
' row with that key does not exist
End If

You may also want to look into using either the SortedList or SortedDictionary class. Both of these are implementations of binary trees. It's hard to say which of all of these options is going to be fastest in your particular scenario. It all depends on the type of data, how often the index needs to be re-built, how often you search it, how many rows are in the DataTable, and what you need to do with the found items. The best thing to do would be to try each one in a test case and see which one works best for what you need.

Check if value exists in Datatables

Absolutely. Using the cell().data() API call you can get/set cell data like this:

Get

 var cellData = Table.cell(rowIndex, columnIndex).data();

Set

Table.cell(rowIndex, columnIndex).data() = cellData;

Check if row exists in DataTable?

If you use a typed DataSet, I.e. declared in design time, the "linq Contains method" takes a typed DataRow. The default IEqualityComparer will compare all values in the DataRow. (Which is normally useless, since you should have a key defined).

DataSet1 ds = new DataSet1();
DataSet1.DataTable1Row row = ds.DataTable1.AddDataTable1Row(bla, bla);
bool exists = ds.DataTable1.Contains(row);

Check if value exist in datatable

Since there's only a single value returned, you could do this:

string value = (string)command.ExecuteScalar();

Anyway the DataTable has a collection .Rows of DataRow elements.Each DataRow corresponds to a record row in your database.

If you want to access a single value, do something like this:

   foreach(DataRow row in dtbl.Rows)
{
if(row["ColumnName"].ToString()=="Name")
{
}
}

Check if column exists when iterating datatable?

You can use:

ReportMonth = record.Table.Columns.Contains("Month") 
? Convert.ToString(record["Month"])
: string.Empty;

Convert.ToString(object) returns string.Empty if the object is null so we don't need to check it.

Here is a speed performance optimization:

bool hasName = dataTable.Columns.Contains("Name");
bool hasDate = dataTable.Columns.Contains("Date");
bool hasimageUrl = dataTable.Columns.Contains("imageUrl");
bool hasMonth = dataTable.Columns.Contains("Month");
bool hasLon = dataTable.Columns.Contains("Lon");
bool hasLat = dataTable.Columns.Contains("Lat");

var envelope = new
{
// use: ReportMonth = hasMonth ? ... : ... ;
}

Check if value exists from a datatable to another datatable with LIKE operator

The condition specified in a Where clause must evaluate to a Booelan. In your case, your condition is a query that returns a list of rows, which is not a Boolean. What you care about is whether that list has any items, which is what the Any method is for:

Where (From BDT2 In BDT1 Where BDT2(2).Contains(ADT2(3))).Any()


Related Topics



Leave a reply



Submit