c# - How to Compare Two dataTable and Update non matched rows in a DataTable which gives the resultDataTable? -
masterdata
id name
1 central
2 east
3 east central
4 east coastal
5 north
6 north west
7 south
8 south central
9 west
data received
id name value
1 central 125.65
5 north 553.21
i want result followes
id name value
1 central 125.65
2 east 0.0
3 east central 0.0
4 east coastal 0.0
5 north 553.21
6 north west 0.0
7 south 0.0
8 south central 0.0
9 west 0.0
please note datatable how can result
let datatable
declared following:
var dt1 = new datatable(); dt1.columns.add(new datacolumn("id", typeof(int))); dt1.columns.add(new datacolumn("name", typeof(string))); var dt2 = new datatable(); dt2.columns.add(new datacolumn("id", typeof(int))); dt2.columns.add(new datacolumn("name", typeof(string))); dt2.columns.add(new datacolumn("value", typeof(double)));
you can join , want linq objects:
var query = r1 in dt1.asenumerable() join r2 in dt2.asenumerable() on r1.field<int>("id") equals r2.field<int>("id") r3 r4 in r3.defaultifempty() select new { id = r1.field<int>("id"), name = r1.field<string>("name"), value = r4 == null ? 0.00 : r4.field<double>("value") };
with ienumerable<anonymous_type>
can datatable
object back, using todatatable<t>
extension method:
public static class enumerabletodatatableconverter { public static datatable todatatable<t>(this ienumerable<t> items) { datatable datatable = new datatable(typeof(t).name); //get properties propertyinfo[] props = typeof(t).getproperties(bindingflags.public | bindingflags.instance); foreach (propertyinfo prop in props) { //setting column names property names datatable.columns.add(prop.name); } foreach (t item in items) { var newrow = datatable.newrow(); (int = 0; < props.length; i++) { //inserting property values datatable rows newrow[props[i].name] = props[i].getvalue(item, null); } datatable.rows.add(newrow); } //put breakpoint here , check datatable return datatable; } }
you can datatable
query
following statement:
var result = query.todatatable();
Comments
Post a Comment