1

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?

2 Answers 2

4

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

Example:

   MessageBox.Show(r1.First());
Sign up to request clarification or add additional context in comments.

Comments

3

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));

3 Comments

You can't use an index on IEnumerable. First casting it to a List would work.
ElementAt() would also be possible
Thanks, both @Carra and you. I choose your answer because of your better answer. Thanks a lot you saved my time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.