c# - Querying 2 datatables in a dataset -
i have 2 datatables named 'dst' , 'dst2'. located in dataset 'urenmat'. mayority of data in 'dst'. contains column named 'werknemer'. contains value corresponds row in 'dst2'. column named 'nummer'.
what need way left outer join both datatables dst.werknemer , dst2.nummer linked, , new datatable created contains 'dst2.naam' linked 'dst.werknemer' along other columns 'dst'.
i have looked everywhere , still can't seem te find right answer question. several sites provide way using linq in situation. have tried using linq not skilled @ this.
i tried using 101 linq samples: http://code.msdn.microsoft.com/101-linq-samples-3fb9811b
urenmat = dataset. dst = a, b, c, d, werknemer. dst2 = nummer, naam.
i used following code '101'.
var query = contact in dst.asenumerable() join order in dst2.asenumerable() on contact.field<string>("werknemer") equals order.field<string>("nummer") select new { = order.field<string>("a"), b = order.field<string>("b"), c = order.field<string>("c"), d = order.field<string>("d"), naam = contact.field<decimal>("naam")};
i don't know change 'contact' , 'order' , can't seem find out how save datatable again.
i sorry if these stupid questions have tried solve myself appears i'm stupid:p. thank in advance!
ps. using c# code, dataset , datatables typed.
if want produce projected dataset of dst
left outer joined dst2
can use linq expression (sorry don't work in linq query syntax you'll have use lambda syntax instead).
var query = dst.asenumerable() .groupjoin(dst2.asenumerable(), x => x.field<string>("werknemer"), x => x.field<string>("nummer"), (contact, orders) => new { contact, orders }) .selectmany(x => x.orders.defaultifempty(), (x, order) => new { = order.field<string>("a"), b = order.field<string>("b"), c = order.field<string>("c"), d = order.field<string>("d"), naam = x.contact.field<decimal>("naam") });
because projected dataset cannot save datatable. if saving desired want load affected row, update desired fields, save changes.
// untyped var row = dst.rows.find(keyvalue); // typed var row = dst.findby...(keyvalue); // update field row.setfield("a", "some value"); // save row's changes row.acceptchanges(); // or after changes table have been made, save table dst.acceptchanges();
normally if need perform loading , saving of (projected) data, orm (like entity framework, or linq-to-sql) best tool. however, using datatable
's in case , i'm not sure if can link orm these (though seems possible).
Comments
Post a Comment