Skip to main content

Notifications

Dynamics 365 general forum

Multi join using LINQ method syntax

Posted on by

We're developing an application that lets end users essentially write and execute their own Dynamics queries. Since these queries are built dynamically, we are creating them using LINQ expressions. This requires using the LINQ method syntax instead of query syntax, but most of the documentation and community examples use the query syntax. This has been fine for the simple queries we've been doing so far, but now we are trying to do multiple joins. Unfortunately, I could not find any method sytax examples for this on the internet, and I'm getting an unhelpful error that I also can not find anywhere on the internet. As an example, the following multi-join statement using the query syntax works, but the method query results in an error:

var query = from a in dataContext.CreateQuery("account")
join c in dataContext.CreateQuery("contact")
on a["accountid"] equals c["parentcustomerid"]
join i in dataContext.CreateQuery("incident")
on c["contactid"] equals i["customerid"]
where i.GetAttributeValue<string>("description").StartsWith("A")
select new
{
Contact = new
{
LastName = c["lastname"]
}
};

var methodQuery = dataContext.CreateQuery("account")
.Join(dataContext.CreateQuery("contact"), a => a["accountid"], c => c["parentcustomerid"], (a, c) => new { a = a, c = c })
.Join(dataContext.CreateQuery("incident"), jt => jt.c["contactid"], i => i["customerid"], (jt, i) => new { a = jt.a, c = jt.c, i })
.Where(jt => jt.i.GetAttributeValue<string>("description").StartsWith("A"));

This is the error I get:
System.InvalidOperationException : The projection property does not match an existing entity binding.

Is there some kind of limitation in the LINQ to dynamics provider that I'm missing here? Are the names and formatting wrong somehow?

  • Verified answer
    Luke Colburn Profile Picture
    Luke Colburn on at
    RE: Multi join using LINQ method syntax

    Posting on another forum made me realize that I needed to add a select on the end of the method syntax query. I was trying to debug things by building up the query incrementally, but the LINQ to Dynamics provider requires that you have a select statement before a join query can be evaluated at all.

  • Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: Multi join using LINQ method syntax

    In this case, you can use early binding rather than late binding.

  • Luke Colburn Profile Picture
    Luke Colburn on at
    RE: Multi join using LINQ method syntax

    I know that will work, but I can't use that syntax for the work I'm doing. The LINQ method syntax is at least somewhat supported, as Microsoft has at least a few examples using it: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/org-service/linq-query-examples#use-a-method-based-linq-query-with-a-where-clause. As best I can tell, the second example is the correct way to convert the working query syntax into method syntax, but it doesn't work.

  • Dynamics365 Rocker Profile Picture
    Dynamics365 Rocker 7,755 on at
    RE: Multi join using LINQ method syntax

    Hi,

    You can use following linq:

    var query = from a in dataContext.CreateQuery("account")

    join c in dataContext.CreateQuery("contact")

    on a["accountid"] equals c["parentcustomerid"]

    join i in dataContext.CreateQuery("incident")

    on c["contactid"] equals i["customerid"]

    where i.GetAttributeValue<string>("description").StartsWith("A")

    select new

    {

    Contact = new

    {

    LastName = c["lastname"]

    }

    };

    Your second linq is not correct as per CRM late binding standard.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans