LINQ: Join on multiple conditions

Here's a sample of a LEFT OUTER JOIN in LINQ using two conditions:
MyDataContext db = new MyDataContext();
string username = "test";

IEnumerable<MyType> query = from c in db.MyCategories
join s in db.MySettings on new { Username=username, CategoryId=c.CategoryId } equals new { Username=s.UserId, CategoryId=s.CategoryId } into tmp
from ss in tmp.DefaultIfEmpty()
orderby c.CategoryName
select new MyType(c.CategoryId, c.CategoryName, ss.UserId == null);
Notice the part after on:
join alias in tablename on new { Name1=alias.Field1, Name2=alias.Field2 } equals new { Name1=alias.Field1, Name2=alias.Field2 }

Please note that I'm mixing the conditions in my example using one value from a table and one string from the code. It's also important that you set the same names one each side of the equals

Related posts:

Comments

comments powered by Disqus