Multiple Join Fields

Jul 10, 2009 at 5:46 PM

Hi,

Can you give an example on how to join one table to another on multiple join fields? (I wasn't able to find one int he sample app).

var Results = (from c in linkCrmQuery.Linq<contact>()
                         join a in linkCrmQuery.Linq<account>()
                                    on c.new_unit.Value equals a.new_unit.Value
                                    and c.owningbusinessunit.Value equals a.owningbusinessunit.Value //not sure how to do the second join field
                                    orderby c.createdon descending
                                    where c.contactid.Value == contactId
                                    && c.statecode.Value == 0).Take(5).ToList();

Thanks!!

Coordinator
Jul 11, 2009 at 8:55 AM

http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/e09b50af-5ab4-4cfb-9617-ef2be339c17f

... don't know how LiqntoCRM will feel about this, but give it a show.

Michael

Dec 16, 2009 at 7:00 PM

Hell Guys,

I tried to run this query

(from addressItem in crmQueryProvider.Linq<customeraddress>()
    join contactItem in crmQueryProvider.Linq<contact>()
        on new {a1 = addressItem.parentid.Value, a2 = addressItem.customeraddressid.Value } 
            equals new {a1 = contactItem.contactid.Value, a2 = contactItem.address2_addressid.Value }
    where addressItem.objecttypecode.Value == "2"
    select addressItem).ToList();

and I got System.NullReferenceException: Object reference not set to an instance of an object. The stack trace is:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at LinqtoCRM.QueryFormatter.VisitJoin(MethodCallExpression join) in D:\1Projects\CRMTest-3.5\LinqToCRM\QueryFormatter.cs:line 660
   at LinqtoCRM.QueryFormatter.VisitMethodCall(MethodCallExpression m, FetchFilters& filters) in D:\1Projects\CRMTest-3.5\LinqToCRM\QueryFormatter.cs:line 75
   at LinqtoCRM.QueryFormatter.VisitMethodCall(MethodCallExpression m) in D:\1Projects\CRMTest-3.5\LinqToCRM\QueryFormatter.cs:line 43
   at LinqtoCRM.ExpressionVisitor.Visit(Expression exp) in D:\1Projects\CRMTest-3.5\LinqToCRM\ExpressionVisitor.cs:line 61
   at LinqtoCRM.QueryFormatter.VisitWhere(MethodCallExpression where) in D:\1Projects\CRMTest-3.5\LinqToCRM\QueryFormatter.cs:line 613
   at LinqtoCRM.QueryFormatter.VisitMethodCall(MethodCallExpression m, FetchFilters& filters) in D:\1Projects\CRMTest-3.5\LinqToCRM\QueryFormatter.cs:line 61
   at LinqtoCRM.QueryFormatter.VisitMethodCall(MethodCallExpression m) in D:\1Projects\CRMTest-3.5\LinqToCRM\QueryFormatter.cs:line 43
   at LinqtoCRM.ExpressionVisitor.Visit(Expression exp) in D:\1Projects\CRMTest-3.5\LinqToCRM\ExpressionVisitor.cs:line 61
   at LinqtoCRM.ExpressionVisitor.VisitExpressionList(ReadOnlyCollection`1 original) in D:\1Projects\CRMTest-3.5\LinqToCRM\ExpressionVisitor.cs:line 168
   at LinqtoCRM.ExpressionVisitor.VisitMethodCall(MethodCallExpression m) in D:\1Projects\CRMTest-3.5\LinqToCRM\ExpressionVisitor.cs:line 158
   at LinqtoCRM.QueryFormatter.VisitMethodCall(MethodCallExpression m, FetchFilters& filters) in D:\1Projects\CRMTest-3.5\LinqToCRM\QueryFormatter.cs:line 69
   at LinqtoCRM.QueryFormatter.VisitMethodCall(MethodCallExpression m) in D:\1Projects\CRMTest-3.5\LinqToCRM\QueryFormatter.cs:line 43
   at LinqtoCRM.ExpressionVisitor.Visit(Expression exp) in D:\1Projects\CRMTest-3.5\LinqToCRM\ExpressionVisitor.cs:line 61
   at LinqtoCRM.QueryFormatter.Format(Expression expression) in D:\1Projects\CRMTest-3.5\LinqToCRM\QueryFormatter.cs:line 33
   at LinqtoCRM.CrmQueryProvider.Translate(Expression expression) in D:\1Projects\CRMTest-3.5\LinqToCRM\CrmQueryProvider.cs:line 179
   at LinqtoCRM.CrmQueryProvider.<ExecuteQuery>d__4`1.MoveNext() in D:\1Projects\CRMTest-3.5\LinqToCRM\CrmQueryProvider.cs:line 131
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at CRMTest.Helper.GetContactWithAddresses2() in D:\1Projects\CRMTest-3.5\Helper.cs:line 446
   at CRMTest.Program.ExecuteMethod(ExecutingMethod method, String methodName) in D:\1Projects\CRMTest-3.5\Program.cs:line 76
   at CRMTest.Program.Main(String[] args) in D:\1Projects\CRMTest-3.5\Program.cs:line 38

It seems to me that LinqtoCRM does not support anonimous classes in join.

May be you have another idea how to join by multiple fields?

Best regards,

    Dmitry