Linq to Entities Distinct on Column without Anonymous Type

白昼怎懂夜的黑 提交于 2019-12-11 08:37:47

问题


I am using Entity Framework 5.0 and I wish to return a list of objects, however, I want to perform a DISTINCT on one of the properties on each object within the list.

I know there are a few questions similar to mine already on Stackoverflow, however, I am still struggling with this one.

Currently my query looks like this

public IList<tblcours> GetAllCoursesByOrgID(int id)
{
     return _UoW.tblcoursRepo.All.Where(c => c.tblCourseCategoryLinks.Any(cl => cl.tblUnitCategory.tblUnit.ParentID == id))
            .OrderBy(c => c.CourseTitle)
            .ToList();
}

However, I need to perform a DISTINCT on the property MainHeadingID to remove any objects already with the same ID, but still returning the entire object with all its properties.

Preferably, I would like to return IList, as you can see from my method, and not an Anonymous Type.

Any help with this is greatly appreciated.

Thanks.


回答1:


Have you tried using GroupBy?

_UoW.tblcoursRepo.All.GroupBy(c => c.MainHeadingId)
                     .Select(g => g.FirstOrDefault())


来源:https://stackoverflow.com/questions/15087531/linq-to-entities-distinct-on-column-without-anonymous-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!