December 2007
Intermediate to advanced
896 pages
19h 57m
English
You have two sets of data from different data domains, and you want to be able to combine the data and work with it.
Use LINQ to bridge across the disparate data domains. LINQ is intended to be used in the same manner across different data domains and supports combining those sets of data with join syntax.
To demonstrate this, we will join an XML file full of Categories with the data from a database (Northwind) with Products and combine the two to create a new set of data for product information that holds the product name, the category description, and the category name:
Northwind dataContext = new Northwind(Settings.Default.NorthwindConnectionString);
ProductsTableAdapter adapter = new ProductsTableAdapter();
Products products = new Products();
adapter.Fill(products._Products);
XElement xmlCategories = XElement.Load("Categories.xml");
var expr = from product in products._Products
where product.Units_In_Stock > 100
join xc in xmlCategories.Elements("Category")
on product.Category_ID equals int.Parse(xc.Attribute("CategoryID").Value)
select new
{
ProductName = product.Product_Name,
Category = xc.Attribute("CategoryName").Value,
CategoryDescription = xc.Attribute("Description").Value
};
foreach (var productInfo in expr)
{
Console.WriteLine("ProductName: " + productInfo.ProductName +
" Category: " + productInfo.Category +
" Category Description: " + productInfo.CategoryDescription);
}The new set of data is printed to the console, ...
Read now
Unlock full access