Friday, January 10, 2014

Asp.net

var persons = new DataTable();
 
string lastName = string.Empty;
 
DataRow[] foundRows = persons.Select("Person_Id = 100");
if (foundRows.Length == 1)
{
    lastName = foundRows[0]["Last_Name"].ToString();
}
 
now with LINQ, I have created the following method:
        
public static T GetFirstResultValue(DataTable table,string colToSearch, string colToReturn, TY searchValue) where TY: IComparable
{
    T ret = default(T);
 
    IEnumerable rows = from row in table.AsEnumerable()
                                        where row.Field(colToSearch).CompareTo(searchValue) == 0
                                        select row;
 
    if (rows.Count() == 1)
    {
        ret = (T) rows.First()[colToReturn];
    }
 
    return ret;
}
 

1 Comment:

  1. Duc Nguyen said...
    foreach (DataRow o in dt.Select("", "NAME ASC"))
    {
    Console.WriteLine("\t" + o["SSN"] + "\t" + o["NAME"] + "\t" + o["ADDR"] + "\t" + o["AGE"]);
    }

Post a Comment