0
我有一个类的成分,其中有一个属性Items
的类型List<Ingredient>
。
在我的一个网页,我使用一个GridView来显示所有的成分,按首字母分组:LINQ查询与按组和按升序排列不起作用
<Page.Resources>
<CollectionViewSource x:Name="IngredientsViewSource" IsSourceGrouped="True" ItemsPath="Items"/>
</Page.Resources>
加载页面时,该CollectionViewSource的来源属性设置是这样的:
this.IngredientsViewSource.Source = CurrentData.Ingredients.GroupedItems;
GroupedItems
是要准备好使用的配料类,这需要以财产Items
和订单和群体一切的属性:
public object GroupedItems
{
get
{
if (this.Items != null)
{
return from IngredientIteration in this.Items
//orderby IngredientIteration.Name ascending
group IngredientIteration
by IngredientIteration.FirstLetter
into IngredientGroup
//orderby IngredientGroup.Key ascending
select new {
FirstLetter = IngredientGroup.Key,
Items = IngredientGroup
};
}
else
{
return null;
}
}
private set { }
}
这是工作得很好。现在我想对结果进行排序,因为现在第一个字母的顺序都搞乱了。但是,当我删除两个orderby
子句前面的注释标记时,它变得非常奇怪。现在留下像这样的orderby子句会导致正确排序的组,但只显示每个组的第一个项目。
但是,当我改变升序为降序时,一切都按预期工作:组按降序排序,显示所有项目,并且项目在每个组内按降序排序。
这对我来说毫无意义,为什么要降职但升职不是?我在这里错过了什么吗?
在我看来,你的'GroupedItems'将有'Items'作为'IGrouping <...>',我怀疑这不是你想要的。 “GroupedItems”中每个条目的实际数据结构是什么? –
我想收集这些匿名对象的每个字符串'FirstLetter'和'List'' Items'。 –