2

I have following function -

public static DataTable getDetails(PersonContext context)
{
DataTable dt = new DataTable();
IQueryable<Person> query = from p in context.Persons.Include("Employee")
                                                    .Include("Manager")
                                                    .Include("Activity")
                           where p.Activity.IsActive
                           select p;
var sorted = query.ToArray().OrderByDescending(p=>p.Activity.DateCreated);
dt = (DataTable)sorted;
return dt;
}

I can't test it. My question is - will this function work. If no what changes should I make in it?

Update

public static DataTable getDetails(PersonContext context)
{
DataTable dt = new DataTable("Details");
dt.Columns.Add("Name");
dt.Columns.Add("Department");
dt.Columns.Add("IsManager");
IQueryable<Person> query = from p in context.Persons.Include("Employee")
                                                    .Include("Manager")
                                                    .Include("Activity")
                           where p.Activity.IsActive
                           select p;
var sorted = query.ToArray().OrderByDescending(p=>p.Activity.DateCreated);
foreach(Person p in sorted)
{
    dt.Rows.Add(p.Name, p.Employee.Department,p.Manager.IsManager);
}
    return dt;
}
RTRokzzz
  • 235
  • 1
  • 4
  • 14
  • 1
    As is, this won't work as `sorted` will be ok type `IQueryable`. I believe you should better sort the results at the consuming side, not within the datatable itself. Sorting is a presentation layer's feature, not datalayer – Steve B Jan 07 '13 at 09:02
  • Check those solutions [here](http://www.dreamincode.net/forums/topic/85368-c%23-convert-object-array-into-datatable/). Hope this will help. – Marcin Buciora Jan 07 '13 at 09:00

3 Answers3

3

OrderByDescending() returns an IEnumerable not a DataTable

This link answers what seems to be the same question. LINQ to DataTable Easy and Fast

Community
  • 1
  • 1
Moriya
  • 7,750
  • 3
  • 35
  • 53
3
dt = (DataTable)sorted;

is an invalid cast. The correct way is to add the returned array to dt using
dt.Rows.Add(query.ToArray().OrderByDescending(p=>p.Activity.DateCreated).toArray());.
You can find more information at Adding Data To Datatable

prthrokz
  • 1,120
  • 8
  • 16
  • 1
    Create a DataRow from Person first. DataRow dr = dt.NewRow(); Then add the necessary values to the row, dr["Name"] = p.Name;dr["Dept"] = p.Employee.Department and so on. Then finally dt.Rows.Add(dr); All this happens within the foreach loop. – prthrokz Jan 07 '13 at 10:03
  • Thanks @prthrokz. I am following your way. However, I can't test it, but I am assuming, It will work now. – RTRokzzz Jan 07 '13 at 10:09
1

If the return type of OrderByDescending wasDataTable then the cast is not needed. The compiler knows the type of the method return type at compile time.

However OrderByDescending doesn't return a DataTable You need to construct it.

MBen
  • 3,956
  • 21
  • 25