This is a migrated thread and some comments may be shown as answers.

Multiple joins with "where" condition(s)

1 Answer 165 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Caleb Tucker
Top achievements
Rank 1
Caleb Tucker asked on 14 Dec 2009, 06:53 PM
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

1 Answer, 1 is accepted

Sort by
0
Damyan Bogoev
Telerik team
answered on 15 Dec 2009, 01:10 PM
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.
Tags
LINQ (LINQ specific questions)
Asked by
Caleb Tucker
Top achievements
Rank 1
Answers by
Damyan Bogoev
Telerik team
Share this question
or