gpt4 book ai didi

c# - Asp.net LINQ groupby 和 orderBy 日期不检索预期的输出

转载 作者:太空宇宙 更新时间:2023-11-03 16:43:02 24 4
gpt4 key购买 nike

我正在使用 linq2sql 开发一个 asp.net mvc3 应用程序。

我有一个 SiteLog 对象类型列表,其中还包含每个对象:一个名为 CLREExceptionType 的字符串和一个名为 EntryDate 的日期。此列表包含在字典中:

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

然后我在函数中的同一个 Controller 中填充词汇表:

   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 次)最后发生时间:22.10.1989 19:30
Exception2(2 次)最后发生于 20.5.1980 14.50

数据库2
异常 1(22 次)最后发生时间:21.10.1989 19:30
Exception2(2 次)最后发生于 20.5.1980 14.50

然后列表突然更新为 database2 中的一个新条目,所以我必须将这个数据库放在最上面:
数据库2
异常 1(1 次)上次发生时间:29.07.2011 12.00
异常 1(22 次)最后发生时间:21.10.1989 19:30
Exception2(2 次)最后发生于 20.5.1980 14.50

数据库 1:
异常 1(22 次)最后发生时间:22.10.1989 19:30
Exception2(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/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>

提前发送

最佳答案

解决这个问题的一种方法是在您拥有数据后从 L2SQL 切换到 L2Objects,然后再次分组以便您可以排序:

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

这将为您提供 KeyDB 的组列表,按照组最后一个异常的顺序排列。

要使用这些结果,您可以使用嵌套的 foreach 循环:

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

关于c# - Asp.net LINQ groupby 和 orderBy 日期不检索预期的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6870038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com