Tony Johansson wrote:
I just wonder if you have any examples of creating a LINQ query between a
DataSet and an Xml file.
Here is a sample join between a DataTable and an XDocument:
DataTable table1 = new DataTable("example");
DataColumn col1 = new DataColumn("k", typeof(int));
table1.Columns.Add(col1);
DataColumn col2 = new DataColumn("foo", typeof(string));
table1.Columns.Add(col2);
for (int i = 0; i < 5; i++)
{
DataRow row = table1.NewRow();
row[col1] = i;
row[col2] = "foo " + i.ToString();
table1.Rows.Add(row);
}
XDocument doc = new XDocument(
new XElement("root",
new XElement("bar",
new XAttribute("k", 1),
new XAttribute("bar", "bar 1")
),
new XElement("bar",
new XAttribute("k", 3),
new XAttribute("bar", "bar 3")
),
new XElement("bar",
new XAttribute("k", 10),
new XAttribute("bar", "bar 10")
)));
var query = from row in table1.AsEnumerable()
join bar in doc.Root.Elements("bar") on
row.Field<int>("k") equals (int)bar.Attribute("k")
select new { key = row.Field<int>("k"), foo =
row.Field<string>("foo"), bar = (string)bar.Attribute("bar") };
foreach (var item in query)
{
Console.WriteLine("key: {0}; foo: {1}; bar: {2}",
item.key, item.foo, item.bar);
}
Output is
key: 1; foo: foo 1; bar: bar 1
key: 3; foo: foo 3; bar: bar 3
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/