Skip to main content

Notifications

Announcements

No record found.

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 57

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

  • Suggested answer
    pavanmanideep Profile Picture
    pavanmanideep 1,329 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
    binoyk 57 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
    pavanmanideep 1,329 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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans