web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Query Expression with Link entity inner join on 2 columns

(0) ShareShare
ReportReport
Posted on by

Hello All, 

i want to achieve the below using query expression link entity 

"Select * FROM icms_icmssubjectmatrixmapping A INNER JOIN ava_subjectmatrix B ON

A.icms_subcategory= B.ava_subjectmatrix_subjectname AND

A.icms_category= B.ava_subjectmatrix_categoryname"

my code below has to check another and condition on join 

string subjectmatrixid = string.Empty;
bool ismappedcategory = true;
// CrmServiceAdapter OrgService=new CrmServiceAdapter();
SchemaMappingController mappingController = new SchemaMappingController();
QueryExpression queryExpression = new QueryExpression("icms_icmssubjectmatrixmapping");
queryExpression.LinkEntities.Add(new LinkEntity("icms_icmssubjectmatrixmapping", "ava_subjectmatrix", "icms_subcategory", "ava_subjectmatrix_subjectname", JoinOperator.Inner));
queryExpression.LinkEntities[0].EntityAlias = "Link";

queryExpression.Criteria.Conditions.Add(new ConditionExpression("icms_msoswitchboardcategory", ConditionOperator.Equal, (object)"Animal Issues"));
queryExpression.Criteria.Conditions.Add(new ConditionExpression("icms_msoswitchboardsubcategory", ConditionOperator.Equal, (object)"Animal Cruelty/Welfare"));
queryExpression.Criteria.Conditions.Add(new ConditionExpression("icms_ismappedcategory", ConditionOperator.Equal, (object)ismappedcategory));

 //queryExpression.LinkEntities[0].LinkCriteria.AddCondition("ava_subjectmatrix_categoryname", ConditionOperator.Equal, icms_category);

queryExpression.LinkEntities[0].Columns = new ColumnSet("ava_subjectmatrixid", "ava_subjectmatrix_subject");
EntityCollection entitiesJS1 = CrmServiceAdapter.OrgService.RetrieveMultiple(queryExpression);

how to achive this with CRM query expression

please help 

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    Hi ,

    I don't think you can link to multiple columns using Query Expression.

    So either you :

    1- do a join on only one colum like you did on subcategory  and then filter the results l l locally after

    2- you can  use the OrganizationContext with Linq to Sql .

    var orgService= CrmServiceAdapter.OrgService ;

    using (var svcContext= new OrganizationServiceContext(orgService))

               {

    var query_where3 = from A in svcContext.icms_icmssubjectmatrixmappingSet

                            join B in svcContext.ava_subjectmatrixSet

                            on

    A.icms_subcategory  equals  B.ava_subjectmatrix_subjectname AND

    A.icms_category equals B.ava_subjectmatrix_categoryname

                            //where .... ( add conditions here

                            select new

                            {

                               //columns to retrieve

                            };

    }

    check this link form more help on the query : msdn.microsoft.com/.../gg334593.aspx

  • Community Member Profile Picture
    on at

    Hello

    thanks for the reply :)

    Is there any way to USE fetchXML  instead queryexpression to achive the above ,

    i want to try without LINQ

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Linked Entities (either via Fetch Xml or Query expression), are based on unique identifier, so I am pretty sure the answer would be no for what you are trying to do.

  • ashlega Profile Picture
    34,477 on at

    You can use non-id fields in the conditions, but the problem is that you can only link on one set of fields. Anything else, and you can only filter using specific values, not entity fields. Which pretty much crosses out any kind of 2-field-based linkage

  • Thomas David Dayman Profile Picture
    11,323 on at

    You can do FetchExpression instead. Its much easier to use when doing complicated linked entities.

    string fetchxml = "<Your Fetch as a string>"

    EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchxml));

    tracer.Trace("There are {0} entities found", result.Entities.Count);

    foreach (var c in result.Entities)
    {

    //Get Values

    }

  • ashlega Profile Picture
    34,477 on at

    Actually, just got an idea.. if you created a calculated text field on both entities, you might try populating it with a combination of names (category name + subject name), and, then, you might use those calculated fields to match the entities to each other (you`d have a field to match on).

  • Suggested answer
    Community Member Profile Picture
    on at

    Thanks Alex,

    I did the same way, i created a flag field with the combination of the columns , and for time being working, later i change to LINQ

    thank you all for your help

  • Community Member Profile Picture
    on at

    Hello,

    Is there any way atleast to do order by for link entity columns ???

  • Community Member Profile Picture
    on at

    Hello Thomas,

    is there any way to do orderby to the aliased columns in queryexpression ??

    i used queryexpression.Order.Add(new orderexpression ("Link.ColumnName",OrderType.Descending));

    but throwing error saying that column not found with the table :(

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans