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 :
Customer experience | Sales, Customer Insights,...
Suggested Answer

An efficient way of mapping between Fetch XML result and c# class

(0) ShareShare
ReportReport
Posted on by 100

I am trying to see if someone has figured out how to map between fetch xml results to a c# class. i.e., like what Automapper does.

Here is an example of some of my code. Some of my fetchxml has 50 to 60 fields been returned from different entities with inner, left outer joins

public static WorkingDuesDetail GetSalesDetail(Entity src)
{
SalesDetail entity = new SalesDetail();

entity.XXX_XXXID = ((EntityReference)((AliasedValue)src["c.XXX_XXXXXnumber"]).Value).Id;
entity.XXX_XXXXIdNumber = ((AliasedValue)src["c.XXX_XXXXid"]).Value.ToString();
entity.XXX_XXXXXNumberYomiName = !src.Contains("c.XXX_XXXXidnumber_XXX_XXXXidnumberyominame") ? string.Empty;

}

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    I had task similar to yours and I used Attributes for that purpose - www.infoworld.com/.../how-to-work-with-attributes-in-c.html

  • Yassin Khan Profile Picture
    100 on at

    Hi,

    Can you provide a concrete example on what you ended up doing?

    Thanks

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Unfortunately I can't provide you the sourcecode because it belongs to customer but here are main steps I perform:

    1. I created class that describes mapping between source property in result of FetchXml Query run to property of entity. Something like following:

        public class MappingAttribute : Attribute
        {
            public string SourceAttribute { get; private set; }
    
            public MappingAttribute(string sourceAttribute)
            {
                SourceAttribute = sourceAttribute;
            }
        }
    

    2. I created a base class that contained logic that mapped attributes from source to destination using reflection and attributes:

        public abstract class EntityBase
        {
            protected EntityBase(Entity crmRecord)
            {
                var mappingProperties =
                    GetType().GetProperties().Where(p => Attribute.IsDefined(p, typeof(MappingAttribute)));
    
                foreach (var p in mappingProperties)
                {
                    var mappingAttribute =
                        p.GetCustomAttributes(true).FirstOrDefault(a => a.GetType() == typeof(MappingAttribute)) as MappingAttribute;
    
                    if (mappingAttribute == null)
                        continue;
    
                    var crmFieldName = mappingAttribute.SourceAttribute;
    
                    if (!crmRecord.Contains(crmFieldName))
                        continue;
    
                    var crmValue = crmRecord[crmFieldName];
    
                    if (crmValue is EntityReference)
                        crmValue = ((EntityReference)crmValue).Name;
                    else if (crmValue is OptionSetValue)
                        crmValue = crmRecord.FormattedValues[crmFieldName];
                    else if (crmValue is DateTime)
                        crmValue = ((DateTime) crmValue).ToString("MM/dd/yyyy hh:mm tt").Replace('.', '/');
    
                    if (!(crmValue is int) && !(crmValue is string) && !(crmValue is Guid))
                        continue;
    
                    p.SetValue(this, crmValue);
                }
            }
        }

    In my task I needed only labels so I assumed that target fields always will be string. In your case it could be different so you will have to work on polishing.

    3. I inherited my destination class from EntityBase and added property that were marked with my custom attribute:

        public class Contact : EntityBase
        {
            public Contact(Entity crmRecord)
                : base(crmRecord)
            {
            }
            
            [MappingAttribute("firstname")]
            public string FirstName { get; set; }
    
            [MappingAttribute("lastname")]
            public string LastName { get; set; }
    
            [MappingAttribute("fullname")]
            public string fullname { get; set; }
    	}

    4. Use that class with results of fetchxml:

    var myContact = new Contact(crmContact);

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 74 Super User 2025 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 31 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans