Linq – Join Example
I find I’m getting forgetful, so I’m posting a few bits of programming logic here to remind myself if I ever forget things again. This might be a common occurrence in the future, but I have yet to decide if I should mix work with pleasure.
—
Linq is a fully featured query language, which can be used to query & filter data in arrays, enumerable classes, relational DBs, and XML. I deal mostly with relational databases, so this simple example is related to a JOIN between 2 tables.
public static void SimpleJoinExample { var productCats = from p in Products join c in Category on p.CategoryId equals c.Id select new { p.Id, p.Description,c.Name }; foreach (var pc in productCats ) { Console.WriteLine("Product: {0} Category: {1}", i.Description, i.Name); } } public class Category { public int Id { get; set; } public string Description { get; set; } } public class Product { public int Id { get; set; } public string Name { get; set; } public int CategoryId { get; set; } }
Pro LINQ: Language Integrated Query in C# 2010 |
LINQ to Objects Using C# 4.0: Using and Extending LINQ to Objects and Parallel LINQ (PLINQ) (Addison-Wesley Microsoft Technology Series) |
Liked this post? Subscribe to my RSS feed and get loads more!