Reza Paidar
Reza Paidar

Reputation: 883

select single value from linq

I Have a dataTable called dt that filled from Database. the SQL Command is:

SELECT s.keyName,s.value FROM setting as s

after I fill dataTable I want to select a single value from filled dt. So I tried to do some linq that here I show:

if (dtSetting.Rows.Count > 0)
  {
      var r1 = (from r in dtSetting.AsEnumerable()
       where r.Field<string>("keyName") == "logoName"
       select r.Field<string>("value"));

       MessageBox.Show(r1.ToString());
  }

but I can't see the value of s.value and r1.show says that

system.data.enumerableRowCollection`1[system.String]

How can I implement this code to get a real value of the specific column?

Upvotes: 1

Views: 1065

Answers (2)

kgzdev
kgzdev

Reputation: 2885

r1 variable is IEnumarable, so you can get the value by applying index . You can use First() and ElementAt() too.

 if (r1.Count() > 0)
    MessageBox.Show(r1.ToList()[0].ToString());
    //MessageBox.Show(r1.First());
    //MessageBox.Show(r1.ElementAt(0));

Upvotes: 3

Carra
Carra

Reputation: 17964

Your r1 is an IEnumerable < string >. You should select an item from it.

Example:

   MessageBox.Show(r1.First());

Upvotes: 4

Related Questions