【发布时间】:2016-08-27 13:40:22
【问题描述】:
我需要计算ObservableCollection 中可用的联赛。问题是联赛名称重复,所以我需要删除重复项。其实我是这样的:
private static ObservableCollection<Models.Event> _matches = new ObservableCollection<Models.Event>();
public ObservableCollection<Models.Event> Matches
{
get { return _matches; }
}
基本上我有一系列赛事,现在每个赛事都有一个关联的联赛。我的想法是创建另一个名为 LeaguesCount 的属性:
public int LeaguesCount
{
//remove duplicate record from _matches here
}
并返回可用的联赛,但还有另一个问题。因为每个联赛都不一样,所以我可以:
Premier League x3
Serie A x5
Super League x4
...
我需要将计数分开。注意到这个联盟在国家名称中是关联的,所以我可以在_matches 有两个具有这种结构的事件:
事件 (1)
match_name = 'Foo';
match_nation = 'England';
match_league = 'Premier League';
事件(2)
match_name = 'Test';
match_nation = 'England';
match_league = 'Premier League';
事件(3)
match_name = 'Another event';
match_nation = 'England';
match_league = 'Championship';
所以您如何在England 国家/地区看到三个具有Premier League 联赛名称和Championship 联赛名称的赛事,我需要从国家England 获得所有联赛的计数,当然避免重复。
事件的结构基本上是这样的:
public class Event : ViewModel
{
private string _matchNation,
_matchLeague,
_matchName;
}
每个字段都有一个关联的属性。与此问题相关的属性是:
public string MatchNation
{
get { return _matchNation; }
set
{
_matchNation = value;
OnPropertyChanged();
}
}
public string MatchLeague
{
get { return _matchLeague; }
set
{
_matchLeague = value;
OnPropertyChanged();
}
}
public string MatchName
{
get { return _matchName; }
set
{
_matchName = value;
OnPropertyChanged();
}
}
想要的结果:
England -> League count (2) [Championship, Premier League]
主要问题是不同的国家名称,因为我在 xaml 中绑定源是这样的:
<CollectionViewSource Source="{Binding Matches}" x:Key="GroupedItems">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="MatchNation" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
在GroupHeaderTemplate:
<TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="White" FontSize="22" VerticalAlignment="Bottom" />
<TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Orange" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
但 ItemCount 实际上返回所有项目计数(事件)而不是没有重复的联赛,这是一个错误。
【问题讨论】:
-
我认为您要么打错字,要么有逻辑问题。您按
MatchNation分组 -> 只有 England 所以结果 (3) 是正确的。你应该改为MatchLeague -
@lokusking 不是一个合乎逻辑的问题,我需要将活动分组为国家,所以在我的组描述中我有这个组织:
Nations->Leagues->Events。现在我需要计算所有国家的联赛数量(不重复)。我不知道有什么不清楚的地方,请告诉我,我会尽力解释得更好,谢谢。