Multiple joins with "where" condition(s)

2 posts, 0 answers
  1. Caleb Tucker
    Caleb Tucker avatar
    2 posts
    Member since:
    Dec 2009

    Posted 14 Dec 2009 Link to this post

    I'm having trouble with a LINQ query that joins on multiple tables.

    This query throws a NullReferenceException when I try to iterate my addresses variable.  The query works as expected and doesn't throw an exception if I remove "where p.PersonId == 1".

    var addresses = from p in scope.Extent<CorePerson>()
        join pa in scope.Extent<CorePersonAddress>() on p.PersonId equals pa.PersonId
        join a in scope.Extent<CoreAddress>() on pa.AddressId equals a.AddressId
        where p.PersonId == 1
        select new
        {
            p.FirstName,
            p.LastName,
            a.StreetAddress1
        };

    foreach (var a in addresses)
    {
        Response.Write("<br />" + a.FirstName + " " + a.LastName + " " + a.StreetAddress1);
    }

    Stack Trace:

       at Telerik.OpenAccess.Query.QueryContext.PerformDatabaseQuery(Type type, Expression expression, Int32& number, Boolean exec, Int32 numSkip, Int32 numTake, String aggregateMethod, ResultConverter& convert)
       at Telerik.OpenAccess.Query.QueryContext.PerformDatabaseQueryMultiple[T](Expression expression)
       at Telerik.OpenAccess.Query.ObjectScopeQuery`2.GetEnumerator()
       at ArenaSL.Web.Test.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\calebt\My Documents\Test Projects\ArenaSL\ArenaSL.Web\Test.aspx.cs:line 55
       at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    I can't map CorePersonAddress (a many-to-many) table as a Collection in OpenAccess because this is a many to many table that contains more data than just two ids and a sequence column.

    I'm using the latest internal build (version 2009.3.1203.1) since I read some other posts that LINQ joins were improved in this version.

    Any ideas?  Thanks

  2. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 15 Dec 2009 Link to this post

    Hello Caleb Tucker,

    We are sorry for the inconvenience. This is a known issue in the LINQ implementation which was fixed. The fix will be part of the next release, which will be available in few weeks.
    In fact there is a workaround which you can use. You should place the where clause after the first join:

    var addresses = from p in scope.Extent<CorePerson>()
    join pa in scope.Extent<CorePersonAddress>() on p.PersonId equals pa.PersonId
    where p.PersonId == 1
    join a in scope.Extent<CoreAddress>() on pa.AddressId equals a.AddressId
    select new
    {
        p.FirstName,
        p.LastName,
        a.StreetAddress1
    };

    I think that will help you.

    Best wishes,
    Damyan Bogoev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft banner
Back to Top