.net - How to store information from different datatables? -
i have 2 datatables (first rows of tables like):
first table 'differences':
person1| 0 | -2 | 1 |8
second table 'age':
person1| 30 | 20 | 2 | 12
the information need each person top3 'differences' values (sorted ascendingly) there corresponding ages. e.g.:
table person1: 8 | 1 | 0 12| 2 | 30
i can sql query , in r use listobject new in vb.net know best way , store these information (per person) in vb.net.
if you're happy anonymous type , linq-fu, try this:
dim differences = new datatable() dim age = new datatable() each t in {differences, age} each v in {"key", "a", "b", "c", "d"} t.columns.add(v, if(v="key", gettype(string),gettype(integer))) next next differences.rows.add("person1", 0, -2, 1, 8) age.rows.add("person1", 30, 20, 2, 12) differences.rows.add("person2", 4, 5, 6, 7) age.rows.add("person2", 1, 2, 3, 4) dim result = d_row in differences.asenumerable() group join a_row in age on a_row("key") equals d_row("key") rows = group let match = rows.first() select new { .key = d_row("key"), .values = d_row.itemarray.skip(1).zip(match.itemarray.skip(1), function(a, b) tuple.create(a, b)) _ .orderbydescending(function(t) t.item1) _ .take(3) _ .toarray() }
result:
the idea join datatables, zip integer values pairs, sort pairs, , take first 3 pairs of each group.
a shorter, slower approach omit join , use like
dim result = d_row in differences.asenumerable() let match = age.asenumerable().single(function(r) r("key") = d_row("key")) select new { ... }
note omitted null-checks brevity in examples.
in response comment:
... select new { .key = d_row("key"), .values = d_row.itemarray.zip(age.columns.cast(of datacolumn), function(t, c) tuple.create(c.columnname, t)) _ .skip(1) _ .zip(match.itemarray.skip(1), function(a, b) tuple.create(a.item2, b, a.item1)) _ .orderbydescending(function(t) t.item1) _ .take(3) _ .toarray() }
Comments
Post a Comment