Hi
I have below code but it is not sorting by Date
C#
List Result = (from t in context.Sessions
where t.GroupID == GroupID
orderby t.SessionDate
select t).ToList();Thanks
Hi
I have below code but it is not sorting by Date
List Result = (from t in context.Sessions
where t.GroupID == GroupID
orderby t.SessionDate
select t).ToList();Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tuhin PaulRank #7 · 71.9K reputationPosted Apr 2, 2023, 9:50 AM EDT
If you only need to access the SessionDate property and not the entire Session object, use Select to project the results to a new collection of DateTime objects
var Result = context.Sessions .Where(t => t.GroupID == GroupID) .OrderBy(t => t.SessionDate) .Select(t => t.SessionDate) .ToList();Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 2, 2023, 8:00 AM EDT
SessionDateproperty of theSessionclass is of typeDateTimeor a compatible type. Sorting may not work correctly if the property is not of a compatible type.SessionDateproperty are not null or uninitialized. If they are, you may need to filter out those sessions before sorting the list.ascendingkeyword to theorderbyclause to explicitly specify the sorting order:List Result = (from t in context.Sessions where t.GroupID == GroupID orderby t.SessionDate ascending select t).ToList();OrderByextension method instead of theorderbyclause:If none of these suggestions work, you may need to provide more information about the problem, such as the data types and values of the relevant properties, any error messages or exceptions, and the version of .NET you are using.
Tuhin PaulRank #7 · 71.9K reputationPosted Apr 2, 2023, 9:50 AM EDT
Use implicit typing (var) to improve readability
var Result = context.Sessions .Where(t => t.GroupID == GroupID) .OrderBy(t => t.SessionDate) .ToList();