EF和Linq order使用两个参数

本文关键字:两个 参数 Linq order EF | 更新日期: 2023-09-27 18:10:28

我用的是EF 4和c#。

我需要用属于两个不同实体的两个属性来排序这个查询的结果

在我的例子中,我想按gt.GroupTypeId和它的subset by cnt.ContentId排序。

PS:我不确定我的标题是否合适,如果你认为不合适,让我知道我会改变它:-)

from cnt in context.CmsContents
            from gt in cnt.CmsGroupsTypes
            join t in context.CmsTypes
            on cnt.TypeContent equals t.TypeContent
            join m in context.CmsModes
            on cnt.ModeContent equals m.ModeContent
            orderby gt.GroupTypeId // Problem here
            select new
            {
            cnt.ContentId,
            cnt.Title,
            gt.TypeGroup,
            gt.GroupTypeId,
            TypeContentDescription = t.Description,
            ModeContentDescription = m.Description,
            cnt.IsPublished
            };

EF和Linq order使用两个参数

简单示例:

var orderedList = cnt.OrderBy(x => x.GroupTypeId).ThenBy(x => x.ContentId);