2016-07-23 89 views
-1

我有一個與一個國家相關的匹配列表。現在,在迭代我給你這個國家的每一場比賽,因此,例如:如何防止ItemCount出現重複?

Team A = Italy 
Team B = Italy 

我結合這個在這樣的GridView控件相匹配:

<Expander IsExpanded="True" Background="#4F4F4F"> 
    <Expander.Header> 
    <StackPanel Orientation="Horizontal" Height="22"> 
     <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" /> 
     <TextBlock Text=" match" FontSize="22" Foreground="White" FontStyle="Italic" VerticalAlignment="Bottom" /> 
    </StackPanel> 
    </Expander.Header> 
    <ItemsPresenter /> 
</Expander> 

name是國家的名字,反正問題是ItemCount搶了兩次意大利。我需要在xaml中防止這種情況,並且不顯示重複的項目,這可能嗎?

+2

您沒有提供足夠的細節。我需要成爲諾查丹瑪斯才能回答 – Liero

+1

你可以在你設置'ItemCount'的地方發佈代碼嗎? – mariosangiorgio

+0

XAML在這裏看起來大體上不相關......它只顯示您已經計算出的內容。如何填充列表的C#代碼? –

回答

0

你的問題是模糊的,但我猜你根據你的城市名稱沒有按你的結果。

1

我打算繼續,並假設您有一個標有Team的某種類型的通用List

試試這個:

List<Team> teams = new List<Team> 
{ 
    new Team {Name = "Italy"}, 
    new Team {Name = "France"}, 
    new Team {Name = "Italy"} 
}; 

var distinctList = teams.Select(team => team.Name) 
         .Distinct() 
         .Select(team => new Team {Name = team}) 
         .OrderBy(team => team.Name) 
         .ToList(); 

要非常小心,因爲這不是針對性能進行優化,這將只是簡單地給你你想要的結果。如果你的數據集非常大,不要使用它,否則你將需要補償潛在的加載時間。您也可能想要包含IEqualityComparer的某些類別類型以適應StringComparison。然後比較器將作爲參數傳遞給Distinct()

至於ItemCount中只是簡單的設置變量在你的視圖模型,一旦你已經獲得了distinctList

this.ItemCount = distinctList.Count();