Linq to Entities Distinct on Column without Anonymous Type [英] Linq to Entities Distinct on Column without Anonymous Type

查看:194
本文介绍了Linq to Entities Distinct on Column without Anonymous Type的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实体框架5.0 ,我希望返回一个对象列表,但是,我想对每个对象上的一个属性执行 DISTINCT列表。



我知道有一些类似于我已经在 Stackoverflow 的问题,但是我仍然在努力解决这个问题。 / p>

目前我的查询看起来像这样

  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();
}

但是,我需要执行 DISTINCT 属性 MainHeadingID 以删除已经具有相同ID的对象,但仍返回整个对象及其所有属性



最好,我想返回 IList ,您可以从我的方法中看到,不是匿名类型



非常感谢任何帮助。



谢谢。

解决方案

您是否尝试使用 GroupBy

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


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.

解决方案

Have you tried using GroupBy?

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

这篇关于Linq to Entities Distinct on Column without Anonymous Type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆