我正在使用 linq2sql 开发 asp.net mvc3 应用程序。
我有一个 SiteLog 对象类型列表,其中还包含每个对象:名为 CLRExceptionType 的字符串和名为 EntryDate 的日期。该列表包含在字典中:
private Dictionary<string, List<SiteLog>> dataBaseList = new Dictionary<string, List<SiteLog>>();
DataClasses1DataContext db = new DataClasses1DataContext("string1");
DataClasses1DataContext db2 = new DataClasses1DataContext("string2");
我将词汇表填充到函数中的同一控制器中:
private void CreateDictionary()
{
dataBaseList.Add("db", db.SiteLogs.ToList());
dataBaseList.Add("db2", db2.SiteLogs.ToList());
}
然后我有这个 Linq 查询:
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);
这应该给我以下输出:
数据库1:
异常 1(22 次)最后发生时间:1989 年 10 月 22 日 19:30
异常 2(2 次)最后发生时间 20.5.1980 14.50
数据库2
异常 1(22 次)最后发生时间:1989 年 10 月 21 日 19:30
异常 2(2 次)最后发生时间 20.5.1980 14.50
然后突然列表被更新为数据库中的新条目,所以我必须将此数据库放在顶部:
数据库2
异常 1(1 次)上次发生时间:2011 年 7 月 29 日 12 点
异常 1(22 次)最后发生时间:1989 年 10 月 21 日 19:30
异常 2(2 次)最后发生时间 20.5.1980 14.50
数据库1:
异常 1(22 次)最后发生时间:1989 年 10 月 22 日 19:30
异常 2(2 次)最后发生时间 20.5.1980 14.50
所以基本上,我可以按日期对数据库中的日志进行排序,但我无法按最后发生的日期对数据库进行排序,我该如何解决这个问题?
查看代码(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/[email protected] /cdn-cgi/l/email-protection&[email protected] /cdn-cgi/l/email-protection&[email protected] /cdn-cgi/l/email-protection';">
@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