Click here to Skip to main content
15,446,232 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
var Q1 = (ds.Tables[1].AsEnumerable()
                .Select(r => r["ATTRITION_RATE"])
                ).ToList();

            var Q1a = (ds.Tables[1].AsEnumerable()
                .Select(r => r["ATTRITION_RATE"])
                );

            var Q1b = (ds.Tables[1].AsEnumerable()
                .Select(r => r["ATTRITION_RATE"])
                ).ToString();


ATTRITION_RATE column has the value 12.45 (data type : double).

Results:
Q1--it is returning a list which holds the value
Q1a --system.data.enumerablerowcollection
Q1b--System.Data.EnumerableRowCollection`1[System.Object]

Here I want to get the direct value from the data table, how to achieve this.

What I have tried:

I am new to LINQ query stuff,
tried this method but no use
var q1 = ds.Tables[1].AsEnumerable().Select(x => x.Field<double>("ATTRITION_RATE"));
           var q2 = ds.Tables[2].AsEnumerable().Select(x => x.Field<double>("ATTRITION_RATE"));
           var q3 = ds.Tables[3].AsEnumerable().Select(x => x.Field<double>("ATTRITION_RATE"));
           var q4 = ds.Tables[4].AsEnumerable().Select(x => x.Field<double>("ATTRITION_RATE"));
Posted
Updated 5-Dec-20 6:13am

1 solution

OK, the last one is never going to work: unless a class specifically implements a ToString override method the default object is used - which returns the fully qualified name of the type, not anything to do with the content.
So even a simple List will give you nothing useful:
C#
List<int> myList = new List<int> { 1, 2, 3 };
Console.WriteLine(myList.ToString());
Will only give you:
System.Collections.Generic.List`1[System.Int32]


The others return exactly what you asked them to: they went through the entire DataTable, extracted the "ATTRITION_RATE" column values, and put them into an anonymous type collection - in the first case you converted that to a List, in the second you didn't.

If you want a single value from a collection rather than a new collection of items, then don't use Select - use FirstOrDefault[^] if you have a condition, or just get the data directly:
C#
double value = myDataTable.Rows[0].Field<double>("ATTRITION_RATE");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900