Iterate through a DataTable to find items in a List object?

Since I am repeating a DataTable , I need to check each of its DataRow objects for items in the general List row .

I found a blog post using the List Find method along with the delegate, but while this example has a separate class (Person), I'm trying to do something like the following using an instance of the string object :

// My definition of the List object.
List<string> lstAccountNumbers = new List<string>();
...

// I populate the List via its Add method.
...

foreach (DataRow drCurrentRow in dtMyDataTable.Rows) 
{
    if (lstAccounts.Find(delegate(string sAccountNumber) { return sAccountNumber == drCurrentRow["AccountNumber"]; })
    {
        Found_DoSomething();
    }
    else
    {
        NotFound_DoSomethingElse();
    }
}

However, with this syntax, I get "Can't implicitly convert the type" string "to" bool "for the if block .

Can someone clarify what I'm doing wrong, and what is the best way to accomplish what I'm trying to do?

+5
5

. . Exists Not Find. , , bool.

if (lstAccounts.Exists(delegate(string sAccountNumber) { return sAccountNumber == drCurrentRow["AccountNumber"]; })
+3

?

foreach (DataRow drCurrentRow in dtMyDataTable.Rows) 
{
    if (lstAccounts.Contains(drCurrentRow["AccountNumber"].ToString()))
    {
        Found_DoSomething();
    }
    else
    {
        NotFound_DoSomethingElse();
    }
}
+1

if (lstAccounts.Find.

Find , , if bool.

Exists Find.

+1

, ==. if .

+1

linq, , col ..

System; System.Collections; System.Collections.Generic; System.Data; System.Linq; System.Web; System.Web.UI; System.Web.UI.WebControls;

WebApplication1 {   public partial class _Default: System.Web.UI.Page   {       protected void Page_Load ( , EventArgs e)       {            DataTable = DataTable();           table.Columns.Add( "col1", typeof (string));

        DataRow row;
        row = table.NewRow();
        row["col1"] = "123";
        table.Rows.Add(row);
        row = table.NewRow();
        row["col1"] = "456";
        table.Rows.Add(row);

        LinqList<DataRow> rows = new LinqList<DataRow>(table.Rows);
        // do a simple select
       DataRow [] selectedRows = (from r in rows where (string)r["col1"] == "123" select r).ToArray();

        if(selectedRows.Length > 0)
        {
            lable1.Text = "success";
        }
        else
        {
            lable1.Text = "failed";
        }
    }
}


// simple wrapper that implements IEnumerable<T>
internal class LinqList<T> : IEnumerable<T>, IEnumerable
{
    IEnumerable items;

    internal LinqList(IEnumerable items)
    {
        this.items = items;
    }

    #region IEnumerable<DataRow> Members
    IEnumerator<T> IEnumerable<T>.GetEnumerator()
    {
        foreach (T item in items)
            yield return item;
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        IEnumerable<T> ie = this;
        return ie.GetEnumerator();
    }
    #endregion
}

}

URL- DataTable List?

0

All Articles