Linq Exchange
LINQ and Lamda Expressions Explained

Recommendations


Search


Tags


Say you want to write a join in LINQ but you need to do it on more than one field.  You’d expect you’d just say something like,
on a.ID equals b.ID && a.EmployeeID equals b.EmployeeID
or something like that right?  right?  Wrong.

If you want to do a multi-field join you need to use an anonymous type like so:
using (MyDataContext db = new MyDataContext())
{
    var foo = from a in db.Products
        join b in db.Accessories on a.ProductID equals b.ProductID
        join c in db.BundlePackages on b.PackageID equals c.PackageID
        new { doo=a.ID, goo=b.ID }
        equals new { doo=d.KitItemID, goo=c.PackageID }
        select new
        {
            a.ItemID,a.ProductID,b.PackageID,c.BundleID,
        };
}
Notice that the fields must not only be the same type but they must also be named the same way. 

 

Posted by: LinqMaster
Posted on: 11/07/2008 at 11:11 AM
Tags: ,
Categories: LINQ
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Related posts