Multiple Join Fields in LINQ
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.
loading...
loading...
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.
