Rajan
Rajan

Reputation: 17

Linq in datatable or data set

I have a list<string> and a DataSet. I need to write a Linq query to get the values from dataset or datatable to check if the values are present in List<string>. Please help me in writing the query to get datas from dataset or datatable

i will use foreach after getting the values to check whether the data is present in list<string>

EDIT:

DataSet dsDuplicate = (DataSet) Session["EventDescription"];
DataTable dt = dsDuplicate.Tables[0];
string cellValue = string.Empty;
for (int rowCount = 0; rowCount < gvEventMechanic.Rows.Count; rowCount++)
{
     TextBox textBoxId = (TextBox)gvEventMechanic.Rows[rowCount].Cells[2].FindControl("txtId");
     lstStringId.Add(textBoxId.Text);
}

Upvotes: 0

Views: 1193

Answers (1)

Anthony Pegram
Anthony Pegram

Reputation: 126804

List<string> list = ...
DataTable table = ...

var items = new HashSet<string>(list);
var results = from row in table.AsEnumerable()
              where items.Contains(row.Field<string>("YourColumnName"))
              select row;

foreach (var matchingRow in results)
{
    // do whatever
}

Note: If you need the results to be in the form of another DataTable or DataView (such as for databinding), there are methods for that.

var output = results.CopyToDataTable(); // or
var output = results.AsDataView(); 

Upvotes: 2

Related Questions