Asp.net LINQ groupby and orderBy on dates does not

2019-07-21 16:38发布

问题:

I am working on an asp.net mvc3 application with linq2sql.

I have a List of SiteLog object type containing also for each object: A string named CLRExceptionType and a date named EntryDate. This List is included in a Dictionary:

private Dictionary<string, List<SiteLog>> dataBaseList = new Dictionary<string, List<SiteLog>>();
DataClasses1DataContext db = new DataClasses1DataContext("string1");
DataClasses1DataContext db2 = new DataClasses1DataContext("string2");

And I populate the vocabulary in the same controller in a function:

   private void CreateDictionary()
{   
    dataBaseList.Add("db", db.SiteLogs.ToList());
    dataBaseList.Add("db2", db2.SiteLogs.ToList());
}

Then I have this Linq query:

var result =
dataBaseList.GroupBy(x => x.Key)
          .SelectMany(x =>
                        x.SelectMany(n => n.Value
                                           .GroupBy(g => g.CLRExceptionType)
                                           .Select(g => new
                                                        {
                                                          DB = x.Key,
                                                          Exception = g.Key,
                                                          Count = g.Count(), 
                                                          LastOccured = 
                                                           g.Max(y =>
                                                                 y.EntryDate)
                                                        })))
          .OrderBy(x => x.DB)
          .ThenByDescending(x => x.LastOccured);

That should give me the following output:

Database1:
Exception1 (22 times) last occurred: 22.10.1989 19:30
Exception2 (2 times) last occurredd 20.5.1980 14.50

Database2
Exception1 (22 times) last occurred: 21.10.1989 19:30
Exception2 (2 times) last occurredd 20.5.1980 14.50

Then suddenly the List is updated with a new entry in the database2 so I have to put this database on top:
Database2
Exception1 (1 times) last occurred: 29.07.2011 12.00
Exception1 (22 times) last occurred: 21.10.1989 19:30
Exception2 (2 times) last occurredd 20.5.1980 14.50

Database1:
Exception1 (22 times) last occurred: 22.10.1989 19:30
Exception2 (2 times) last occurredd 20.5.1980 14.50

So basically, I can order the logs in the database By date, but I cannot order the databases by the last date occurred on it, how can I solve this? Viewcode (Index.cshtml):

<div id="results">

@{
    string firstTime = "";
}
 @foreach( var database in Model)
 {
       int currentCol = 0 ; 
        if (!(firstTime == database.DB))
        {
          <br style="clear:both" /><h3> @database.DB </h3>
                currentCol = 0;
        }

           <div class="logContainer" onclick="location.href='/logs/Details?databaseID=@database.DB&exceptionName=@database.Exception&exceptionsOccurred=@database.Count';">

                @if (database.Count > 999)
                {
                    <div class="counter-small"><b>@database.Count</b></div>
                }
                else
                { 
                <div class="counter"><b>@database.Count</b></div> 
                }
                <div class="exceptionName"> Exceptions of Type: @database.Exception</div>
                <div class="date">Siste: @database.LastOccurred</div>
         <hr />   </div>
     currentCol += 1;
          if (currentCol == 2) { //3 columns were displayed, switch row
    currentCol = 0;
    <br style="clear:both" />
}

     firstTime = database.DB; 
}

</div>

Tnx in advance

回答1:

One way to approach this would be to switch from L2SQL to L2Objects after you have the data then group again so you can sort:

var result = dataBaseList.GroupBy(x => x.Key)
          .SelectMany(...)
          .AsEnumerable()
          .GroupBy(x => x.DB)
          .OrderByDescending(g => g.Max(x => x.LastOccured));

This will give you a list of groups with Key of DB, in the order of the groups' last exception.

To consume these results, you would use nested foreach loops:

<div id="results">
  @foreach(var group in Model)
  {
    int currentCol = 0; 
    <br style="clear:both" />
    <h3> @group.Key </h3>

    @foreach(var database in group)
    {
      <div class="logContainer" onclick="location.href='/logs/Details?databaseID=@database.DB&exceptionName=@database.Exception&exceptionsOccurred=@database.Count';">

        @if (database.Count > 999)
        {
          <div class="counter-small"><b>@database.Count</b></div>
        }
        else
        { 
          <div class="counter"><b>@database.Count</b></div> 
        }
        <div class="exceptionName"> Exceptions of Type: @database.Exception</div>
        <div class="date">Siste: @database.LastOccurred</div>
        <hr />

      </div>

      currentCol += 1;
      if (currentCol == 2) { //3 columns were displayed, switch row
        currentCol = 0;
        <br style="clear:both" />
      }
    }
  }
</div>