Iterate through a DataTable to find elements in a List object?
Asked Answered
R

5

6

As I iterate through a DataTable object, I need to check each of its DataRow objects against the items in a generic string List.

I found a blog post using the List's Find method along with a delegate, but whereas that example has a separate class (Person), I'm attempting 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'm receiving "Cannot implicitly convert type 'string' to 'bool'" for the if block.

Could someone please clarify what I'm doing wrong and how best to accomplish what I'm trying to do?

Randell answered 24/3, 2010 at 15:49 Comment(0)
C
3

Same Delegate Different method. You want to use Exists Not Find. Find Returns a value while exists returns a bool.

if (lstAccounts.Exists(delegate(string sAccountNumber) { return sAccountNumber == drCurrentRow["AccountNumber"]; })
Convulsant answered 24/3, 2010 at 15:55 Comment(3)
Could you mark this as the correct answer, that would be awsomeConvulsant
@Darth Continent: do not listen to him he is the devil in fleshDatcha
According to your profile @"SO give me back my rep" you are evil, not I.Convulsant
D
1

why would not this work for you?

foreach (DataRow drCurrentRow in dtMyDataTable.Rows) 
{
    if (lstAccounts.Contains(drCurrentRow["AccountNumber"].ToString()))
    {
        Found_DoSomething();
    }
    else
    {
        NotFound_DoSomethingElse();
    }
}
Datcha answered 24/3, 2010 at 15:55 Comment(0)
Y
1

The problem is the if (lstAccounts.Find part.

This Find will return a string if found and the if is expecting a bool output.

Change your statement to use Exists or compare your original value to the Find result.

Yahiya answered 24/3, 2010 at 16:0 Comment(0)
P
1

The list Find method returns a string so you should compare it using Equals method or ==.In that case the if condition will be fine.

Permeable answered 24/3, 2010 at 16:1 Comment(0)
K
0

try using linq, you can create a helper that takes in col name etc...

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

namespace WebApplication1 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DataTable table = new 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
}

}

taken code from this url Iterate through a DataTable to find elements in a List object?

Koontz answered 24/3, 2010 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.