简体   繁体   中英

How to search in data table using LINQ?

Data table result in grid:

SerialNumber  PartNumber   
000000001     QWERTY
000000002     QERTY

I need to search a particular SerialNumber in the result of my data table using a Textbox control.

Pseudocode example:

If
    SearctTexbox.Text =000000001 
    Message: This Serial is Ok!

Else
    Message: Not Ok

How to do this in LINQ or any other methods?

Why do you need LINQ? You can try with

dataTable.Select("condition");

If SerialNumber column have unique values, so give it a try:

//dt is DataTable
dt.PrimaryKey = new DataColumn[1] { dt.Columns[0] };  // set your primary key
DataRow dRow = dt.Rows.Find(SearchTextbox.Text);
if (dRow != null){
     // you've found it
}
else{
    //sorry dude
}
string massage = dc.Parts.Select(
    o => o.SerialNumber == SearctTexbox.Text).Count()>0 ? "Found it"!"No Find";

try this

var item = from r in Datatable.AsEnumerable()
            where r.Field<int>("SerialNumber") == int.Parse(SearchTextbox.Text.ToString())
            select r.Field<int>("SerialNumber");

if (item == null)
{
   // not found
}
else
{
   // you found it. 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM