Domanda

Come ho scorrere un DataTable oggetto, ho bisogno di controllare ogni suo DataRow oggetti contro gli elementi in una stringa generica List .

Ho trovato un post blog con il metodo Find del List insieme a un delegato, ma che tale esempio ha una classe separata (persona), sto cercando qualcosa di simile al seguente utilizzando un'istanza della strong> stringa < oggetto:

// 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();
    }
}

Tuttavia, con questa sintassi sto ricevendo "Impossibile convertire implicitamente il tipo 'string' a 'bool'" per il se di blocco.

Qualcuno potrebbe chiarire che cosa sto facendo male e il modo migliore per realizzare quello che sto cercando di fare?

È stato utile?

Soluzione

Lo stesso metodo diverso Delegato. Si desidera utilizzare esiste non Trova. Trova Restituisce un valore, mentre esiste restituisce un bool.

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

Altri suggerimenti

perché non sarebbe questo lavoro per voi?

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

Il problema è la parte if (lstAccounts.Find.

Questa Find restituirà una stringa se trovato e la if si aspetta un'uscita bool.

Modificare l'istruzione di utilizzare Exists o confrontare il vostro valore originale sul risultato Find.

La lista dei Trova metodo restituisce un stringa così si dovrebbe confrontarlo con Uguale metodo o == . in tal caso la condizione di se andrà bene.

provare a utilizzare LINQ, è possibile creare un aiutante che prende in nome col ecc ...

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

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

}

preso il codice da questo URL scorrere un DataTable per trovare elementi in una lista obiettare?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top