Asp.net LINQ GROUPBY和ORDERBY约会不检索预期的输出 [英] Asp.net LINQ groupby and orderBy on dates does not retrieve the expected output
问题描述
我的工作与LINQ2SQL一个asp.net MVC3应用程序。
I am working on an asp.net mvc3 application with linq2sql.
我也包含每个对象SiteLog对象类型的列表:名为CLRExceptionType的字符串和一个名为EntryDate日期。此列表包含在词典:
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());
}
然后,我有这样的Linq查询:
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:
数据库1:结果
Exception1(22次)上次发生:1989年10月22日19:30结果
Exception2(2次)最后occurredd 1980年5月20日14.50结果
结果
DATABASE2结果
Exception1(22次)上次发生:1989年10月21日19时三十分结果
Exception2(2次)最后occurredd 1980年5月20日14.50搜索
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
然后突然名单与在DATABASE2新条目更新,所以我不得不把这个数据库之上:
结果
DATABASE2结果
Exception1(1次)上次发生:29.07.2011 12.00结果
Exception1(22次)上次发生:1989年10月21日19:30结果
Exception2(2次)最后occurredd 1980年5月20日14.50结果
结果
数据库1:结果
Exception1(22次)上次发生:1989年10月22日19:30结果
Exception2(2次)最后occurredd 1980年5月20日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
所以基本上,我可以为了在数据库中按日期记录,但发生在它的最后一天我不能为了数据库,我该怎么解决这个问题?
查看code(Index.cshtml):
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提前
推荐答案
接近,这将是你有那么数据组后,从L2SQL切换到L2Objects一种方式再次让您可以排序:
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));
这会给你组列表与
中的组的最后一个顺序例外。 DB的关键
This will give you a list of groups with Key
of DB
, in the order of the groups' last exception.
要消耗这些结果,您可以使用嵌套foreach循环:
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>
这篇关于Asp.net LINQ GROUPBY和ORDERBY约会不检索预期的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!