Linq : select value in a datatable column

前端 未结 7 498
广开言路
广开言路 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: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("LocalCurrency") == true
                            select c.Field("CURID");
    
                foreach (int cid in curId)
                {
                    txtCURID.Text = cid.ToString();
                }
                var curName = from c in dt.AsEnumerable()
                              where c.Field("LocalCurrency") == true
                              select c.Field("CurName");
                foreach (string cName in curName)
                {
                    txtCurrency.Text = cName.ToString();
                }
    

提交回复
热议问题