Linq : select value in a datatable column

前端 未结 7 475
广开言路
广开言路 2020-12-06 09:38

How do you use LINQ (C#) to select the value in a particular column for a particular row in a datatable. The equivalent SQL would be:<

相关标签:
7条回答
  • 2020-12-06 10:03

    Thanks for your answers. I didn't understand what type of object "MyTable" was (in your answers) and the following code gave me the error shown below.

    DataTable dt = ds.Tables[0];
    var name = from r in dt
               where r.ID == 0
               select r.Name;
    

    Could not find an implementation of the query pattern for source type 'System.Data.DataTable'. 'Where' not found

    So I continued my googling and found something that does work:

    var rowColl = ds.Tables[0].AsEnumerable();
    string name = (from r in rowColl
                  where r.Field<int>("ID") == 0
                  select r.Field<string>("NAME")).First<string>();
    

    What do you think?

    0 讨论(0)
  • 2020-12-06 10:06
    var name = from DataRow dr in tblClassCode.Rows where (long)dr["ID"] == Convert.ToInt32(i) select (int)dr["Name"]).FirstOrDefault().ToString() 
    
    0 讨论(0)
  • 2020-12-06 10:11
    var name = from r in MyTable
                where r.ID == 0
                select r.Name;
    

    If the row is unique then you could even just do:

    var row = DataContext.MyTable.SingleOrDefault(r => r.ID == 0);
    var name = row != null ? row.Name : String.Empty;
    
    0 讨论(0)
  • 2020-12-06 10:12

    Use linq and set the data table as Enumerable and select the fields from the data table field that matches what you are looking for.

    Example

    I want to get the currency Id and currency Name from the currency table where currency is local currency, and assign the currency id and name to a text boxes on the form:

    DataTable dt = curData.loadCurrency();
                var curId = from c in dt.AsEnumerable()
                            where c.Field<bool>("LocalCurrency") == true
                            select c.Field<int>("CURID");
    
                foreach (int cid in curId)
                {
                    txtCURID.Text = cid.ToString();
                }
                var curName = from c in dt.AsEnumerable()
                              where c.Field<bool>("LocalCurrency") == true
                              select c.Field<string>("CurName");
                foreach (string cName in curName)
                {
                    txtCurrency.Text = cName.ToString();
                }
    
    0 讨论(0)
  • 2020-12-06 10:15

    I notice others have given the non-lambda syntax so just to have this complete I'll put in the lambda syntax equivalent:

    Non-lambda (as per James's post):

    var name = from i in DataContext.MyTable
               where i.ID == 0
               select i.Name
    

    Equivalent lambda syntax:

    var name = DataContext.MyTable.Where(i => i.ID == 0)
                                  .Select(i => new { Name = i.Name });
    

    There's not really much practical difference, just personal opinion on which you prefer.

    0 讨论(0)
  • 2020-12-06 10:15

    If the return value is string and you need to search by Id you can use:

    string name = datatable.AsEnumerable().Where(row => Convert.ToInt32(row["Id"]) == Id).Select(row => row.Field<string>("name")).ToString();
    

    or using generic variable:

    var name = datatable.AsEnumerable().Where(row => Convert.ToInt32(row["Id"]) == Id).Select(row => row.Field<string>("name"));
    
    0 讨论(0)
提交回复
热议问题