Пример #1
0
        public static void AddRequiredQueryJoins(this XrmService xrmService, QueryExpression query, string source)
        {
            var typeFieldPaths   = xrmService.GetTypeFieldPath(source, query.EntityName);
            var splitOutFunction = source.Split(':');

            if (splitOutFunction.Count() > 1)
            {
                source = splitOutFunction.ElementAt(1);
            }
            var splitTokens = source.Split('.');

            if (typeFieldPaths.Count() == 1)
            {
                query.ColumnSet.AddColumn(typeFieldPaths.First().Value);
            }
            else
            {
                LinkEntity thisLink = null;

                for (var i = 0; i < typeFieldPaths.Count() - 1; i++)
                {
                    var lookupField = typeFieldPaths.ElementAt(i).Value;
                    var path        = string.Join(".", splitTokens.Take(i + 1)).Replace("|", "_");
                    if (i == 0)
                    {
                        var targetType    = typeFieldPaths.ElementAt(i + 1).Key;
                        var matchingLinks = query.LinkEntities.Where(le => le.EntityAlias == path);

                        if (matchingLinks.Any())
                        {
                            thisLink = matchingLinks.First();
                        }
                        else
                        {
                            thisLink             = query.AddLink(targetType, lookupField, xrmService.GetPrimaryKeyField(targetType), JoinOperator.LeftOuter);
                            thisLink.EntityAlias = path;
                            thisLink.Columns     = xrmService.CreateColumnSet(new string[0]);
                        }
                    }
                    else
                    {
                        var targetType    = xrmService.GetLookupTargetEntity(lookupField, thisLink.LinkToEntityName);
                        var matchingLinks = thisLink.LinkEntities.Where(le => le.EntityAlias == path);
                        if (matchingLinks.Any())
                        {
                            thisLink = matchingLinks.First();
                        }
                        else
                        {
                            thisLink             = thisLink.AddLink(targetType, lookupField, xrmService.GetPrimaryKeyField(targetType), JoinOperator.LeftOuter);
                            thisLink.EntityAlias = path;
                            thisLink.Columns     = xrmService.CreateColumnSet(new string[0]);
                        }
                    }
                }
                thisLink.Columns.AddColumn(typeFieldPaths.ElementAt(typeFieldPaths.Count() - 1).Value);
            }
        }