Hi
In below linq query i want to get only distinct records
C#
public List GetStudentsByMentor(Int32 MentorId)
{
List Result = (from t in context.View_SessionDeliveryCalendarDetails_Students
where (MentorId != 0 ? t.MentorID == MentorId : 1 == 1)
select t).ToList();
return Result;
}Thanks

Jignesh KumarRank #91 · 22.9K reputationPosted Apr 6, 2023, 1:44 AM EDT
Hello Ramco,
Have you created below as extension method and build your solution,
public static IEnumerable DistinctBy (this IEnumerable source, Func keySelector) { HashSet seenKeys = new HashSet(); foreach (TSource element in source) { if (seenKeys.Add(keySelector(element))) { yield return element; } } }And try below one after that,
public List GetStudentsByMentor(Int32 MentorId) { List Result = (from t in context.View_SessionDeliveryCalendarDetails_Students where (MentorId != 0 ? t.MentorID == MentorId : 1 ==1) select t).DistinctBy(d => new { d.StudentId }).ToList(); return Result; }Amit MohantyRank #112 · 20K reputationPosted Apr 5, 2023, 11:43 PM EDT
Try this
public List GetStudentsByMentor(Int32 MentorId) { List Result = (from t in context.View_SessionDeliveryCalendarDetails_Students where (MentorId != 0 ? t.MentorID == MentorId : 1 == 1) select t).Distinct().ToList(); return Result; }Ramco RamcoRank #462 · 5.4K reputationPosted Apr 5, 2023, 11:49 PM EDT
Hi Amit
Sorry i want distinct to be by StudentId
Thanks
Amit MohantyRank #112 · 20K reputationPosted Apr 5, 2023, 11:57 PM EDT
public List GetDistinctStudentIdsByMentor(int mentorId) { var result = (from t in context.View_SessionDeliveryCalendarDetails_Students where (mentorId != 0 ? t.MentorID == mentorId : 1 == 1) select t.StudentID).Distinct().ToList(); return result; }Ramco RamcoRank #462 · 5.4K reputationPosted Apr 6, 2023, 12:04 AM EDT
Hi
It is giving error - Cannot implicitly convert system.collections.gereric.list to system.collections.gereric.list
C#
public List GetStudentsByMentor(Int32 MentorId) { List Result = (from t in context.View_SessionDeliveryCalendarDetails_Students where (MentorId != 0 ? t.MentorID == MentorId : 1 == 1) select t.StudentID).Distinct().ToList(); foreach (View_SessionDeliveryCalendarDetails_Student item in Result) { item.Name = item.Name + " - " + item.MobileNo; } return Result; }Thanks
Jignesh KumarRank #91 · 22.9K reputationPosted Apr 6, 2023, 12:08 AM EDT
Hello Ramco,
If you want to do distinct by multiple column then you can create extention method as below,
public static IEnumerable DistinctBy (this IEnumerable source, Func keySelector) { HashSet seenKeys = new HashSet(); foreach (TSource element in source) { if (seenKeys.Add(keySelector(element))) { yield return element; } } }You can use as below in your code,
public List GetStudentsByMentor(Int32 MentorId) { List Result = (from t in context.View_SessionDeliveryCalendarDetails_Students where (MentorId != 0 ? t.MentorID == MentorId : 1 ==1) select t).DistinctBy(d => new { d.MentorId, d.MentorName }).ToList(); return Result; }Ramco RamcoRank #462 · 5.4K reputationPosted Apr 6, 2023, 12:23 AM EDT
Hi Jignesh
It is givine error DistinctBy does not exist in current context. I want distinct by Student id. I want to display studentid,name,mobileno
Thanks