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)

Use linq or fetchxml query with dynamic entity list

(0) ShareShare
ReportReport
Posted on by

I'm building a .net console application to query certain crm records (8.2 on-prem) and save the field values to an xml file.  For example I want to query a contact and get values from 20 fields.  I'm using entity framework 6 and I created a db and table where one of the columns has the 20 fields names that I want to use in my query e.g. firstname, lastname, email, etc..  How can I use the list of fields that I get from my ef context/dbset in a linq query?

var context = new DBContext();
            foreach (var field in context.DBSet)
            {
                Console.WriteLine(field.CrmField);
            }


In code above, field.CrmField are all the field names that I'm trying to use in my linq query below:

var linq_query = from c in orgSvcContext.CreateQuery("contact")									 
									 where (String)c["trigger_field"] == "100000001"
									 select new
									 {
										//Instead of hardcoding these values, I'd like to use the list from ef above
										 contact_id = c["contactid"],										 
										 first_name = c["firstname"],
										 last_name = c["lastname"]
									 };
					foreach (var record in linq_query)
					{
						Console.WriteLine($"Linq: {record.first_name} {record.last_name} - contactid:{record.contact_id}");
					}


*This post is locked for comments

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

    Bump anyone?

  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

    Hi

    I do not normally use Linq as behind the scene it generates QueryExpression and if you do your query directly in query expression, you have full control over the query.

    I would normally do this in similary scenario usign Query Expression

              var context = new DBContext();

               foreach (var field in context.DBSet)

               {

                   Console.WriteLine(field.CrmField);

               }

    QueryExpression qe = new QueryExpression("contact");

    qe.ColumnSet = new ColumnSet(context.DBSet.ToArray()); // Field names not hardcoded

           .. Once you executed the RetrieveMultiple, you will get an Entity Collection,  you can then covert that to / creeate any type of object you want from it really

    Let me know more example on this

  • Suggested answer
    Dynamics365 Rocker Profile Picture
    7,755 on at

    You are using late binding, In this case, You can use FetchXML or query expression.

    In console app,You can use early binding, Linq will be easy with Early binding.

  • Community Member Profile Picture
    on at

    Thank you @Kokulan Eswaranathan.  Can you provide a full code example?  Much appreciated.

  • Verified answer
    Community Member Profile Picture
    on at

    Disregard, I was able to solve it with this:

    string[] arrFields = context.dbset.Select(x => x.CrmField).ToArray();

    And then in QueryExpression:

    ColumnSet = new ColumnSet(arrFields)

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