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,...
Answered

C# - Order Entities in EntityCollection by a DateTime attribute

(0) ShareShare
ReportReport
Posted on by

Hey guys,

I am currently working on some code for a Plug-In and stumbled upon an issue.


I am retrieving records stored in an N:N relationship table with the help of the "QueryByAttribute" function. Then I store the results (GUIDs) in a DataCollection. Afterwards I retrieve each single record from that DataCollection with some columns I require in a "foreach" loop and add each single record retrieved to an "EntityCollection". Now I would like to sort the records stored in that "EntityCollection" by a DateTime attribute that was part of the ColumnSet.

So basically I got an EntityCollection that got like 10 records, each record got a "DateTime" attribute (column) by which I would like to sort that "EntityCollection".


Or should I maybe create an own class containing 1. an entity method and 2. an integer method and then use that class with "List<T>", add the results into that List instead of the EntityCollection and then sort the records by DateTime in a foreach loop storing the "Order" of the whole list in the integer part of the class? Or am I missing something really simple and obvious?

Does anyone have a quick advice or sample code for such a requirement or anything similar?

Appreciate your help, guys

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

    Hello,

    Have you considered alternative approach - usage of QueryExpression or FetchExpression so you can retrieve your data in one call and include ordering there?

    If you anyway want to use your approach I believe you can use something like:

    EntityCollection records;
    
    //code to retrieve all the records here
    
    var sorderRecords = records.Entities.ToList().OrderBy(t => t.GetAttributeValue("fieldnamehere"));
    

  • Community Member Profile Picture
    on at

    Thanks Andrew,

    works perfectly.

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 83 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

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

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans