public static DataTable GetData(string listname) { DataTable table = new DataTable(); DataTable dt = new DataTable(); DataColumn IdProject = dt.Columns.Add("IdProject", typeof(int)); DataColumn Count = dt.Columns.Add("Count", typeof(int)); table = Income(listname); var query = (from b in table.AsEnumerable() group b by b.Field<int>("IdProject") into g select new { IdProject = g.Key, Count = g.Count() }).ToList(); foreach (var x in query) { dt.Rows.Add(x.IdProject, x.Count); } return dt; } 

When debugging on a line with b.Field<int>("IdProject") into g , the exception "Specified cast is not valid" is obtained. But I do not understand what is wrong with casting?

    1 answer 1

    Obviously, in the table table IdProject column has some other type.

    • Oddly enough, but yes. IdProject is formed from sheirpoint and since I put there the number of characters after the coma not “Auto”, but “0”, it was stored as a double. A sudden discovery, thank you. - DisguisePerceptron