0

商品が販売された場所の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>
4

3 に答える 3

1

使用する

CountryCode = cc.Key,

それ以外の

CountryCode = cc.Select(c => c.CountryCode).FirstOrDefault(),

また、CountryCodeをトリミングすると、次のような問題を防ぐことができます。

それで:

group sale by sale.CountryCode.Trim() into cc
于 2012-05-11T14:26:17.350 に答える
1

国コードから余分なスペースを削除してください

List<TopSellingCountries> tsc = (from sale in Sales
                                 where sale.CountryCode != null
                                 group sale by sale.CountryCode.Trim() into cc
                                 select new TopSellingCountries
                                 {
                                     CountryCode = cc.Key,
                                     CountryCount = cc.Count()
                                 })
                                 .OrderByDescending(c => c.CountryCount)
                                 .Take(10)
                                 .ToList();
于 2012-05-11T14:44:38.427 に答える
0

以下でお試しください

List<TopSellingCountries> tsc = (from sale in Sales
                                 where sale.CountryCode != null
                                 group sale by sale.CountryCode into cc
                                 order by cc.Count() descending
                                 select new TopSellingCountries
                                 {
                                     CountryCode = cc.Key,
                                     CountryCount = cc.Count()
                                 }).Take(10).ToList();
于 2012-05-11T14:31:59.697 に答える