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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Use Aggregate function in C# Linq to retrieve data from CRM

(0) ShareShare
ReportReport
Posted on by 61

Hello Community,

I am trying to evaluate max date (latest date) condition and for that I think I could use MAX() function.

From syntax perspective I am not able to find correct way. Here below is my query.

var sfcStatusQuery = (from Carrier in _orgContext.CreateQuery("account")
                                          join SafetyFitnessCert in _orgContext.CreateQuery("mot_safetyfitnesscertificate")
                                            on Carrier["accountid"] equals SafetyFitnessCert["mot_carriernumber"]
                                          where (SafetyFitnessCert["mot_expirydate"] == null && (int)SafetyFitnessCert["statecode"] == 0)
                                          where SafetyFitnessCert.FormattedValues["new_sfcstatusname"].ToString().Equals("Suspended")
                                          where ((int)Carrier["statecode"] == 0 && Carrier["mot_number"].Equals(carrierNumber))
                                          //where SafetyFitnessCert["mot_lastreviseddate"]
                                          select new
                                          {
                                              SafetyCertSuspFlag = "Y"
                                          }).FirstOrDefault();



In above query the commented where condition which I try to correct by using aggregate function MAX. As this query may/may not contain more than one record with "Suspended" status for a particular carrier number.

Please advise.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    pavanmanideep Profile Picture
    1,331 on at
    RE: Use Aggregate function in C# Linq to retrieve data from CRM

    Hi Binoyk,

    Please refer to the below posts which could help you

    https://nishantrana.me/2017/06/02/linq-groupby-sum-and-entitycollection-in-crm/

    https://www.inogic.com/blog/2013/03/aggregate-and-grouping-functions-using-linq-in-crm/

    Hope this helps..

  • binoyk Profile Picture
    61 on at
    RE: Use Aggregate function in C# Linq to retrieve data from CRM

    Thank you Mani.

    Are you pointing towards using FetchXML?

    I know with FetchXML it should be pretty much easy compared to LINQ but prior I shift my implementation, would like to know whether it is possible to use aggregate function in LINQ while working with CRM? (Does CRM allow writing LINQ with aggregates?)

  • Suggested answer
    pavanmanideep Profile Picture
    1,331 on at
    RE: Use Aggregate function in C# Linq to retrieve data from CRM

    Hi Binoyk,

    Yes, you should be able to use MAX functions in Linq queries.

    Use the below link LINQPAD to connect to your CRM instance and generate the query as required.

    LINQPAD for CRM

    Hope this helps.

    Please mark this as verified if this helps...

    Cheers,

    Mani Deep Y

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#3
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans