c# - Convert datatable to list with relation -
i have table(one table) it's illustration - real time table huge.
devisionid employeeid date hr 1130107 3011657 2014-10-11 8 1130107 3011657 2014-10-12 1 1130107 3011660 2014-10-11 8 1130107 3011660 2014-10-12 6
and want convert datatable list of object lists, please show me best way how this.
as illustration this
public class devision { public int id {get;set;} public list<employees> employee {get;set;} devision(int id, list<employees> emp) { id = id; employees = emp; } } public class employees { public int id {get;set;} public list<info> item {get;set;} employees(int id, list<info> info) { id = id; item = info; } } public class info { public datetime date {get;set;} public float hr {get;set;} info(datetime dtime, float hour) { date = dtime; hr = hour; } }
i object linq, , if can not queryable level(but no matter). let's start getting linq
:
var mytab = table.asenumerable(); var itemses = mytab.select(
thankx.
this should want:
list<devision> devisions = table.asenumerable() .select(row => new devision( row.field<int>("devisionid"), new employees( row.field<int>("employeeid"), new info(row.field<datetime>("date"), row.field<float>("hour"))))) .tolist();
note constructors have public
. also, datarow.field
not casting, have provide right type or convert values target types (or alter way table filled).
now have changed question seems want grouping devisionid
, employeeid
, initialize list<employees>
every group. have look:
list<devision> devisions = table.asenumerable() .groupby(row => new { empid = row.field<int>("employeeid"), devid = row.field<int>("devisionid")}) .select(devempgroup => new devision( devempgroup.key.devid, devempgroup .select(row => new employees( devempgroup.key.empid, new info(row.field<datetime>("date"), row.field<float>("hour")))) .tolist())) .tolist();
now property info
in employees
list<info>
should you:
list<devision> devisions = table.asenumerable() .groupby(row => row.field<int>("devisionid")) .select(devgrp => new devision( devgrp.key, devgrp.groupby(row => row.field<int>("employeeid")) .select(g => new employees( g.key, g.select(r => new info(r.field<datetime>("date"), r.field<float>("hour"))) .tolist())) .tolist())) .tolist();
c# linq datatable ienumerable
No comments:
Post a Comment