简体   繁体   中英

Linq to Entities Group By issue

I have a sales database that records in a CountryCode column the 2 character country code of where a good was sold. In the sales database, there is no quantity column, basically every row represents 1 sale, and the information associated with that sale.

I was to be able to display the top selling countries. Here is the linq query I have come up with:

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();

When I output this to my View however, I get a table with the following information:

CountryCode | CountryCount
         US | 196
         IE | 168
         US | 99
         GB | 91
         IE | 57
         AU | 32
         GB | 22
         AU | 18
         CA | 17
         CA | 17

As you can see it doesn't seem to be grouping properly by country code. Does anyone have any ideas how I can overcome this?

EDIT: Here is the code from the View if anyone needs it:

<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>

use

CountryCode = cc.Key,

instead of

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

Also trimming the CountryCode can prevent problems like this:

so:

group sale by sale.CountryCode.Trim() into cc

Make sure you trim excess spaces off of the CountryCode

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();

Please try with the following

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM