Returning datatable using entity framework

后端 未结 5 1523
南方客
南方客 2020-12-09 10:37

I am using entity framework. There is one particular situation in my application where I have to use a stored procedure. Since there are a lot of SQL statements written in t

5条回答
  •  余生分开走
    2020-12-09 11:08

    This example will return a datatable object selecting data from EntityFramework.

    I believe this is the best solution to the goal. However the problem with this solution is that each record is enumerated. You might want to filter the list first then run this from the list to avoid that.

    DataTable dt = new DataTable();
    (from rec in database.Table.AsEnumerable()
                         select new
                         {
                             id = rec.id,
                             name = rec.Name
                             //etc
                         }).Aggregate(table, (dt, r) =>
                         {
                             dt.Rows.Add(r.id, r.Name);
                             return dt;
                         });
    

提交回复
热议问题