Hi
In below code i want to order by name
C#
var Result = (from t2 in context.View_SessionDeliveryCalendarDetails_Students
select new { t2.StudentID, Name = t2.Name + " - " + t2.MobileNo }).Distinct().ToList();Thanks

Amit MohantyRank #112 · 20K reputationPosted Apr 7, 2023, 7:18 AM EDT
var Result = (from t2 in context.View_SessionDeliveryCalendarDetails_Students select new { t2.StudentID, Name = t2.Name + " - " + t2.MobileNo }) .Distinct() .OrderBy(x => x.Name) // Order by Name .ToList();Jignesh KumarRank #91 · 22.9K reputationPosted Apr 7, 2023, 7:19 AM EDT
Hello Ramco,
use below one,
var Result = (from t2 in context.View_SessionDeliveryCalendarDetails_Students orderby t2.Name select new { t2.StudentID, Name = t2.Name + " - " + t2.MobileNo }).Distinct().ToList();Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 7, 2023, 7:44 AM EDT
Try below:
var Result = (from t2 in context.View_SessionDeliveryCalendarDetails_Students select new { t2.StudentID, Name = t2.Name + " - " + t2.MobileNo }) .Distinct() .OrderBy(x => x.Name) .ToList();Tuhin PaulRank #7 · 71.9K reputationPosted Apr 7, 2023, 2:36 PM EDT
Instead of using the + operator to concatenate strings, you can use string interpolation to make the code more concise and easier to read
var Result = (from student in context.View_SessionDeliveryCalendarDetails_Students select new { student.StudentID, Name = $"{student.Name} - {student.MobileNo}" }) .Distinct() .OrderBy(x => x.Name) // Order by Name .ToList();Raj BhattRank #513 · 4.8K reputationPosted Apr 8, 2023, 12:35 AM EDT
Hi You can Try Below Code
For Ascending order
var Result = (from t2 in context.View_SessionDeliveryCalendarDetails_Students select new { t2.StudentID, Name = t2.Name + " - " + t2.MobileNo }) .Distinct() .OrderBy(x => x.Name) .ToList();For Descending order
var Result = (from t2 in context.View_SessionDeliveryCalendarDetails_Students select new { t2.StudentID, Name = t2.Name + " - " + t2.MobileNo }) .Distinct() .OrderByDescending(x => x.Name) .ToList();Tuhin PaulRank #7 · 71.9K reputationPosted Apr 7, 2023, 2:34 PM EDT
Instead of using generic names like t2, consider using names that more accurately describe the purpose of the variables. In this case, you could use names like student or calendarDetail.
Tuhin PaulRank #7 · 71.9K reputationPosted Apr 7, 2023, 2:40 PM EDT
Although query syntax is useful for writing more declarative and readable queries, method syntax can sometimes be more concise and easier to read.
var Result = context.View_SessionDeliveryCalendarDetails_Students .Select(student => new { student.StudentID, Name = $"{student.Name} - {student.MobileNo}" }) .Distinct() .OrderBy(x => x.Name) // Order by Name .ToList();