AFAIK you can join only this way:
var query = from obj_i in set1 join obj_j in set2 on new { JoinProperty1 = obj_i.SomeField1, JoinProperty2 = obj_i.SomeField2, JoinProperty3 = obj_i.SomeField3, JoinProperty4 = obj_i.SomeField4 } equals new { JoinProperty1 = obj_j.SomeOtherField1, JoinProperty2 = obj_j.SomeOtherField2, JoinProperty3 = obj_j.SomeOtherField3, JoinProperty4 = obj_j.SomeOtherField4 }
Basic requirements: Property names, types, and order in the anonymous objects you connect to must match.
You cannot use ANDs OR etc. in connections. Just object1 is equal to object2.
More advanced stuff in this LinqPad example:
class c1 { public int someIntField; public string someStringField; } class c2 { public Int64 someInt64Property {get;set;} private object someField; public string someStringFunction(){return someField.ToString();} } void Main() { var set1 = new List<c1>(); var set2 = new List<c2>(); var query = from obj_i in set1 join obj_j in set2 on new { JoinProperty1 = (Int64) obj_i.someIntField, JoinProperty2 = obj_i.someStringField } equals new { JoinProperty1 = obj_j.someInt64Property, JoinProperty2 = obj_j.someStringFunction() } select new {obj1 = obj_i, obj2 = obj_j}; }
Name addressing and property ordering is simple, addressing types can be achieved using casting / transforming / parsing / calling methods, etc. This may not always work with LINQ to EF or SQL or NHibernate, most method calls will definitely not work and will not work at runtime, so YMMV. This is because they are copied to read-only public properties in anonymous objects, so as long as your expression creates the correct type, the join property - you should be fine.
Zar Shardan Oct 18 '11 at 19:56 2011-10-18 19:56
source share