Null Pointer
Null Pointer

Reputation: 9309

Select a particular value from datatable-C#

I have a table.From that table i am selecting rows Based on the key field of the table .This time I am selecting rows from database based on key 'search'.So i got following rows in my datatable .

Id | key | Value | assignedvalue

1 search count 10

4 search name John

6 search channels 20

I have 3 variables declared

        int count,channels;
        string name;

I want to assign corresponding values to the above variables from that table.

ie, i must get values from data table like shown below

count=10,

channels= 20

name=John

How can i do this?

Upvotes: 0

Views: 26356

Answers (3)

Dave Fancher
Dave Fancher

Reputation: 386

Using LINQ to DataSet gives you better type safety, better compile time checking, and avoids the overhead of parsing the filter expression. You can also take advantage of LINQ's deferred execution to avoid rewriting the query for each of the assignments. You may consider something like this:

var valueName = String.Empty;
var query =
    from r in dt.AsEnumerable()
    where r.Field<string>("Value").Equals(valueName)
    select r.Field<string>("AssignedValue");

valueName = "count";
var count = 0;
int.TryParse(query.FirstOrDefault(), out count);

valueName = "channels";
var channels = 0;
int.TryParse(query.FirstOrDefault(), out channels);

valueName = "name";
var name = query.FirstOrDefault();

In the above sample we change the value of the valueName variable to the name of the value we're looking for in the DataTable then force the query to re-enumerate with the call to FirstOrDefault.

Upvotes: 2

Jay Riggs
Jay Riggs

Reputation: 53593

One way to do what you want to do is by using the DataTable.Select method.

Using this method you'd define a select-like query and apply it to your DataTable. You'd get back an array of DataRow objects that match your query. Here's an example adapted from the example in the MSDN article I linked:

Note: I'm assuming the DataColumn with names is called NameColumn, and the column with the values you want to retreive is called ValueColumn.

    string expression;
    expression = "NameColumn = 'John'";
    DataRow[] foundRows;

    // Use the Select method to find all rows matching the filter.
    foundRows = table.Select(expression);

    // Print column 0 of each returned row.
    for(int i = 0; i < foundRows.Length; i ++) {
        // List out the retreived values.
        Console.WriteLine(foundRows[i]["ValueColumn"]);
    }

If you do want to use LINQ however, you can. See LINQ query on a DataTable.

Upvotes: 1

Fun Mun Pieng
Fun Mun Pieng

Reputation: 6911

What do you mean "search count 10"? if you want to know how many rows you have dt.Rows.Count does the job.

if the column name is FirstName then dr["FirstName"] can be used to get the value. You can loop through and check the value.

What do you mean "search selected All"? If you want to find a row with value in "selected" column to be true, you can check if ((dr["selected"] as bool? ?? false) == true)

Try to improve your question. Explain what you want to achieve. It would be good if you could provide the schema of the table. Otherwise things like "search selected All" doesn't make sense.

Upvotes: 1

Related Questions