Click here to Skip to main content
14,766,280 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
List<Yearly> yearly = dataset.Tables[0].AsEnumerable()
               .Select(row => new Yearly
               {
                   Yearly_Rate = row.Field<float>("ATTRITION_RATE"),
                   Total_EmpCount = row.Field<int>("TOTAL_EMP"),
                   Resigned_EmpCount = row.Field<int>("RESIGNED_EMP")

               }).ToList();


In My Database ATTRITION_RATE is float, TOTAL_EMP is int, RESIGNED_EMP is int

What I have tried:

List<Yearly> yearly = dataset.Tables[0].AsEnumerable()
                .Select(row => new Yearly
                {
                    Yearly_Rate = row.Field<float>("ATTRITION_RATE"),
                    Total_EmpCount = row.Field<Int32>("TOTAL_EMP"),
                    Resigned_EmpCount = row.Field<Int32>("RESIGNED_EMP")

                }).ToList();


but still the same issue
Posted
Updated 1-Dec-20 6:04am
Comments
Richard MacCutchan 1-Dec-20 9:51am
   
Which line produces the error?
   
.Select()
Richard MacCutchan 1-Dec-20 10:50am
   
You need to provide more information.

Use the debugger to look at your data table and it's content: something in it is not a float or an int, or is in the wrong column.

This kind of thing often happens when a DataTable is filled from a badly designed DB which stores numeric data as strings instead of as native numeric values - so when a column gets corrupted data, it shows up later as a casting problem.

You need to look at the data and work out what value is in error before you can go any further - and the start looking at how it got there and what you can do to fix it, then prevent it ever happening again!

Sorry, but we can't do that for you - we have no access to your running code or the data behind it!
   
Comments
   
My table data:

RESIGNED_EMP TOTAL_EMP ATTRITION_RATE
70 1180 5.882
OriginalGriff 1-Dec-20 10:38am
   
And what about all the other rows?
I cross checked the datatype of the table value by using the below line. ATTRITION_RATE column data was double type..

System.Type type2 = ds.Tables[0].Rows[0][2].GetType();


List<Yearly> yearly = dataset.Tables[0].AsEnumerable()
                .Select(row => new Yearly
                {
                    Yearly_Rate = row.Field<double>("ATTRITION_RATE"),
                    Total_EmpCount = row.Field<int>("TOTAL_EMP"),
                    Resigned_EmpCount = row.Field<int>("RESIGNED_EMP")

                }).ToList();


casted the data accordingly, it is working
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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