1

给定以下实体:

Question
 -string Title
 -ICollection<Tag> Tags

Tag
 -string Value
 -string TagType

如何Questions按给定的Valueof排序?这里的问题是 a可以有许多给定的。如果有多个(这在我的场景中很不寻常),我只关心第一个.TagsTagTypeQuestionTagsTagTypeQuestionTagsTag

这是一个示例数据表输出,其中 TagTypes 有自己的列:

Title (Question.Title) | Topic (TagType) | Module (TagType)
-----------------------------------------------------------
Question 1             | Algebra         | Maths

给定一个IQueryable<Question>,我可以发送什么表达式Queryable.OrderBy(...)来实现我的目标?

4

2 回答 2

0
Enumerable.OrderBy(x => x.Tags.Where(x => x.TagType == aTagType).First().Value);
于 2013-10-08T12:47:56.363 回答
0

感谢@James 的建议,但没有奏效。解决方案是使用MinMax为子集合的字段排序:

if (sortDescending)
{
    expr = q => q.Tags
        .Where(t => t.TagType.Equals(sortByTagType, StringComparison.CurrentCultureIgnoreCase))
        .Max(t => t.Value);
}
else
{
    expr = q => q.Tags
        .Where(t => t.TagType.Equals(sortByTagType, StringComparison.CurrentCultureIgnoreCase))
        .Min(t => t.Value);
}
于 2013-10-09T15:38:17.560 回答