LINQ problem, get data from var

AWestEng
AWestEng used Ask the Experts™
on
HI

How do I get the data form the a var variable to a string variable?

  var test = from o in myTable.AsEnumerable()
                     where o.Field<string>("C1") == "Test1"
                     && o.Field<string>("C2") == "Test2"
                     select o.Field<double>("Test3");

String test = var..........;
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Project manager
Commented:
Do you expect one or more records back ?

var test = from o in myTable.AsEnumerable()
                     where o.Field<string>("C1") == "Test1"
                     && o.Field<string>("C2") == "Test2"
                     select o.Field<double>("Test3");

foreach (var Result in test )
            Console.WriteLine("Result: " + Result.ToString());

Author

Commented:
Onre record only

Author

Commented:
one record only
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

For a single record you have to do the same

Do you expect one or more records back ?

var test = from o in myTable.AsEnumerable()
                     where o.Field<string>("C1") == "Test1"
                     && o.Field<string>("C2") == "Test2"
                     select o.Field<double>("Test3");

foreach (var Result in test )
            Console.WriteLine("Result: " + Result.ToString());

Author

Commented:
ok, the only result I will get back is the number 3 or some other number

Author

Commented:
so even if I only have one record in the var "array" I must use foreach ?
yes
Dirk HaestProject manager

Commented:
You can easily take it like this:
(I assume you query just one field)

String test = (string)test[0];

Author

Commented:
Error> Cannot apply indexing with [] to an expression of type 'System.Data.EnumerableRowCollection<double>'
It's a collection, use the foreach loop !
Dirk HaestProject manager
Commented:
You can also alter your query a little bit:

var test = from o in myTable.AsEnumerable()
                     where o.Field<string>("C1") == "Test1"
                     && o.Field<string>("C2") == "Test2"
                     select o.Field<double>("Test3");

String test  = (String )test .Take(1).FirstOrDefault()
Top Expert 2009
Commented:
Here's a more direct version:

string test = (from o in  myTable.AsEnumerable()
                     where  o.Field<string>("C1") == "Test1"
                      && o.Field<string>("C2") == "Test2"
                      select o.Field<double>("Test3")).FirstOrDefault();

Arun
I wouldn't loop if you are expecting only one, nmarun is right in his approach.  I would Actually recommend using .Single() or .SingleOrDefault() - that way it will let you know if it did return more than one by throwing an exception.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial