Search code examples
entity-frameworklinqlinq-to-sqlentity-framework-6

How should I get distinct record in linq?


Hi I have tried below query to get the distinct record but I am not able to get the distinct record from below query.

var query = (from sr in db.StudentRequests
            join r in db.Registrations on sr.RegistrationId equals r.RegistrationId
            join cc in db.Campus on r.CampusId equals cc.CampusId
            join c in db.Classes on sr.ClassId equals c.ClassId
            from tc in db.TutorClasses.Where(t => t.ClassId == sr.ClassId).DefaultIfEmpty()
            from srt in db.StudentRequestTimings.Where(s => s.StudentRequestId == sr.StudentRequestId).DefaultIfEmpty()
            from tsr in db.TutorStudentRequests.Where(t => t.StudentRequestId == srt.StudentRequestId && t.TutorId == registrationid).DefaultIfEmpty()
            where tc.RegistrationId == registrationid
            select new
            {
                StudentRequestId = sr.StudentRequestId,
                RegistrationId = sr.RegistrationId,
                Location = sr.Location,
                PaymentMethod = sr.PaymentMethod,
                CreatedOn = sr.CreatedOn,
                ClassName = c.ClassName,
                CampusName = cc.CampusName,
                StatusId = tsr.StatusId == null ? 1 : tsr.StatusId,
                Time = db.StudentRequestTimings.Where(p => p.StudentRequestId == sr.StudentRequestId)
                        .Select(p => p.FromTime.ToString().Replace("AM", "").Replace("PM", "") + "-" + p.ToTime)
            }).Distinct().ToList().ToPagedList(page ?? 1, 3);

But I am getting error as

The 'Distinct' operation cannot be applied to the collection ResultType of the specified argument.\r\nParameter name: argument

So I had removed the .Distinct() from the above query and below that code I had written code as

 query = query.Distinct().ToList();

But still the duplicate record was showing

enter image description here

So I had tried Group by clause to get distinct record but over there also I am facing the issue.Please Review below code

query = query.ToList().GroupBy(x => new { x.StudentRequestId, x.StatusId, x.Location, x.RegistrationId, x.PaymentMethod, x.CreatedOn, x.ClassName, x.CampusName})
.Select(group => new
{
    StudentRequestId = group.Key.StudentRequestId,
    StatusId = group.Key.StatusId,
    Location = group.Key.Location,
    RegistrationId = group.Key.RegistrationId,                                    
    PaymentMethod = group.Key.PaymentMethod,
    CreatedOn = group.Key.CreatedOn,
    ClassName = group.Key.ClassName,
    CampusName = group.Key.CampusName,                                   
    Time1 = group.Key.Time
});

But I am getting error for time as enter image description here

How can I get the distinct Value? Also make in concern that I am using ToPagedList in the query

The actual issue is coming from Time column, If I remove that column all things are working fine.


Solution

  • I had added Group by clause at the end of the query as suggested by @Rajaji and that worked for me.

     var query = (from sr in db.StudentRequests
                 join r in db.Registrations on sr.RegistrationId equals r.RegistrationId
                 join cc in db.Campus on r.CampusId equals cc.CampusId
                 join c in db.Classes on sr.ClassId equals c.ClassId
                 from tc in db.TutorClasses.Where(t => t.ClassId == sr.ClassId).DefaultIfEmpty()
                 from srt in db.StudentRequestTimings.Where(s => s.StudentRequestId == sr.StudentRequestId).DefaultIfEmpty()
                 from tsr in db.TutorStudentRequests.Where(t => t.StudentRequestId == srt.StudentRequestId && t.TutorId == registrationid).DefaultIfEmpty()
                 where tc.RegistrationId == registrationid
                 select new
                 {
                   StudentRequestId = sr.StudentRequestId,
                   RegistrationId = sr.RegistrationId,
                   Location = sr.Location,
                   PaymentMethod = sr.PaymentMethod,
                   CreatedOn = sr.CreatedOn,
                   ClassName = c.ClassName,
                   CampusName = cc.CampusName,
                   StatusId = tsr.StatusId == null ? 1 : tsr.StatusId,
                   Time = db.StudentRequestTimings.Where(p => p.StudentRequestId == sr.StudentRequestId).Select(p => p.FromTime.ToString().Replace("AM", "").Replace("PM", "") + "-" + p.ToTime)
                   }).ToList().GroupBy(p => new { p.StudentRequestId }).Select(g => g.First()).ToList();