商品が販売された場所の2文字の国コードをCountryCode列に記録する販売データベースがあります。販売データベースには数量列はなく、基本的にすべての行が1つの販売と、その販売に関連する情報を表します。
売れ筋の国を表示できるようになりました。これが私が思いついたlinqクエリです:
List<TopSellingCountries> tsc = (from sale in Sales
where sale.CountryCode != null
group sale by sale.CountryCode into cc
select new TopSellingCountries
{
CountryCode = cc.Select(c => c.CountryCode).FirstOrDefault(),
CountryCount = cc.Count()
}).OrderByDescending(c => c.CountryCount).Take(10).ToList();
ただし、これをビューに出力すると、次の情報を含むテーブルが表示されます。
CountryCode | CountryCount
US | 196
IE | 168
US | 99
GB | 91
IE | 57
AU | 32
GB | 22
AU | 18
CA | 17
CA | 17
ご覧のとおり、国コードごとに適切にグループ化されていないようです。誰かが私がこれを克服する方法について何かアイデアがありますか?
編集:誰かがそれを必要とするならば、これはビューからのコードです:
<table class="normal">
<tr>
<th>Country Code</th>
<th>Country Count</th>
</tr>
<% foreach (var item in Model.TopSellingCountries)
{ %>
<tr>
<td><%: item.CountryCode %></td>
<td><%: item.CountryCount %></td>
</tr>
<% } %>
</table>