Click here to Skip to main content
15,040,212 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"])

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

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

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

Q1--it is returning a list which holds the value

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"));
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:
List<int> myList = new List<int> { 1, 2, 3 };
Will only give you:

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:
double value = myDataTable.Rows[0].Field<double>("ATTRITION_RATE");

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