Searching a particular value in datatable
I am having two datatables and my basic need is to know whether the v开发者_运维问答alue in a datatable is exist on the another datatable.
My first datatable will contains data like this (id,name,unit) Second one like this (id,value). The values may be like this 1-A-b,2-B-c,3-X-d for first one and 1-2,3,5 for second one. Here 1 and 3 are exsting.How can I find the corresponding values using the id.
this should work;
var table = new DataTable();
table.Columns.Add("id", typeof(int));
table.Columns.Add("name");
table.Columns.Add("unit");
var table2 = new DataTable();
table2.Columns.Add("id", typeof(int));
table2.Columns.Add("value");
table.Rows.Add(1, "a Name", "a Unit");
table.Rows.Add(2, "other", "other");
table2.Rows.Add(1, "value");
table2.Rows.Add(4, "other");
var result = table.AsEnumerable().Join(table2.AsEnumerable(), r1 => r1.Field<int>("id"), r2 => r2.Field<int>("id"),
(r1, r2) => new {Id = r1.Field<int>("id"), Value = r2.Field<string>("value") }).ToList();
foreach (var r in result)
Console.WriteLine(r.Id + "|"+ r.Value);
精彩评论