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)

LINQ query to get the first contact?

(0) ShareShare
ReportReport
Posted on by

Hi, I have a plug-in with a LINQ query to get the parent contact of an opportunity:

var tempContact = (from c in ServiceContext.CreateQuery("contact")
	where c.Id.ToString().Equals(contact_guid)
	select c).FirstOrDefault();

Where "ServiceContext" is defined as:

IOrganizationService service = localContext.OrganizationService;
var ServiceContext = new OrganizationServiceContext(service);

I get an error message saying that the query is incorrect. Any suggestions?

Or other query suggestions would be appreciated as well, I'm only doing this with LINQ because that's the only approach I know. I just need to get the contact associated with the opportunity, thanks.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi,

    If you have the ID of the contact you can retrieve it directly instead of querying:

    public void Execute(IServiceProvider serviceProvider)
    {
      IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
      IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
      var contact = service.Retrieve("contact", theId, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
     {...}
    }


    Hope this helps! I'd appreciate if you'd mark this as a Verified answer.

    Thanks,

      Aiden

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