为什么我的 linq 排序不起作用
尝试按 logtype 或按 logType 然后按 DateTime 对日志进行排序,但不起作用。我缺少什么?
public enum LogType
{
Fatal,
Error,
Warn,
Info,
Debug,
None,
}
public class Log
{
public DateTime LoggedDateTime { get; set; }
public string Message { get; set; }
public LogType LoggedType { get; set; }
public override string ToString()
{
return string.Format("LogType: {0} Datetime: {1} Message: {2}", LoggedType, LoggedDateTime, Message);
}
}
class Program
{
static void Main()
{
var logFatal = new Log
{
LoggedDateTime = new DateTime(2011, 2, 22),
Message = "Hi am a Fatal message",
LoggedType = LogType.Fatal
};
var logInfo = new Log
{
LoggedDateTime = new DateTime(2013, 2, 22),
Message = "Hi I am a Info message",
LoggedType = LogType.Info
};
var logError = new Log
{
LoggedDateTime = new DateTime(2010, 2, 22),
Message = "Hi I am a Error message",
LoggedType = LogType.Error
};
var logWarning = new Log
{
LoggedDateTime = new DateTime(2014, 2, 22),
Message = "Hi I am a Warning message",
LoggedType = LogType.Warn
};
List<Log> logs = new List<Log> { logWarning, logError, logInfo, logFatal };
Console.WriteLine("Not Ordered");
Console.WriteLine("");
logs.ForEach(x=>Console.WriteLine(x.LoggedType));
var orderedLogs = logs.OrderBy(x => x.LoggedType == LogType.Fatal)
.ThenBy(x => x.LoggedType == LogType.Error)
.ThenBy(x => x.LoggedType == LogType.Warn)
.ThenBy(x => x.LoggedDateTime).ToList();
Console.WriteLine("Ordered by logType fatal first and NOT WORKING");
Console.WriteLine("");
orderedLogs.ForEach(x=>Console.WriteLine(x.LoggedType));
Console.WriteLine("");
Console.WriteLine("Ordered by logType fatal first and NOT WORKING");//NOT WORKING
List<Log> orderedFatal = logs.OrderBy(x => x.LoggedType == LogType.Fatal).ToList();
orderedFatal.ForEach(x => Console.WriteLine(x.LoggedType));
Console.WriteLine("");
Console.WriteLine("Sorted by datetime AND WORKS"); //THIS IS THE ONLY ONE THAT WORKS
logs.Sort((x,y)=>x.LoggedDateTime.CompareTo(y.LoggedDateTime));
logs.ForEach(x => Console.WriteLine(x.LoggedDateTime));
Console.Read();
}
}
感谢您的任何建议
Trying to order logs by logtype or by logType and thenBy DateTime but does not work.What am I missing?
public enum LogType
{
Fatal,
Error,
Warn,
Info,
Debug,
None,
}
public class Log
{
public DateTime LoggedDateTime { get; set; }
public string Message { get; set; }
public LogType LoggedType { get; set; }
public override string ToString()
{
return string.Format("LogType: {0} Datetime: {1} Message: {2}", LoggedType, LoggedDateTime, Message);
}
}
class Program
{
static void Main()
{
var logFatal = new Log
{
LoggedDateTime = new DateTime(2011, 2, 22),
Message = "Hi am a Fatal message",
LoggedType = LogType.Fatal
};
var logInfo = new Log
{
LoggedDateTime = new DateTime(2013, 2, 22),
Message = "Hi I am a Info message",
LoggedType = LogType.Info
};
var logError = new Log
{
LoggedDateTime = new DateTime(2010, 2, 22),
Message = "Hi I am a Error message",
LoggedType = LogType.Error
};
var logWarning = new Log
{
LoggedDateTime = new DateTime(2014, 2, 22),
Message = "Hi I am a Warning message",
LoggedType = LogType.Warn
};
List<Log> logs = new List<Log> { logWarning, logError, logInfo, logFatal };
Console.WriteLine("Not Ordered");
Console.WriteLine("");
logs.ForEach(x=>Console.WriteLine(x.LoggedType));
var orderedLogs = logs.OrderBy(x => x.LoggedType == LogType.Fatal)
.ThenBy(x => x.LoggedType == LogType.Error)
.ThenBy(x => x.LoggedType == LogType.Warn)
.ThenBy(x => x.LoggedDateTime).ToList();
Console.WriteLine("Ordered by logType fatal first and NOT WORKING");
Console.WriteLine("");
orderedLogs.ForEach(x=>Console.WriteLine(x.LoggedType));
Console.WriteLine("");
Console.WriteLine("Ordered by logType fatal first and NOT WORKING");//NOT WORKING
List<Log> orderedFatal = logs.OrderBy(x => x.LoggedType == LogType.Fatal).ToList();
orderedFatal.ForEach(x => Console.WriteLine(x.LoggedType));
Console.WriteLine("");
Console.WriteLine("Sorted by datetime AND WORKS"); //THIS IS THE ONLY ONE THAT WORKS
logs.Sort((x,y)=>x.LoggedDateTime.CompareTo(y.LoggedDateTime));
logs.ForEach(x => Console.WriteLine(x.LoggedDateTime));
Console.Read();
}
}
Thanks for any suggestions
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的
logWarning
和logError
都有LoggedType = LogType.Error
!另一点:您应该实现/使用
IComparer
对于LogType
...请参阅 MSDN http://msdn.microsoft.com/en-us/library/8ehhxeaf.aspx基本上是这样的:
Both your
logWarning
andlogError
haveLoggedType = LogType.Error
!Another point: you should implement/use an
IComparer
forLogType
... see MSDN at http://msdn.microsoft.com/en-us/library/8ehhxeaf.aspxBascially something like:
您可以按字段 LoggedType 对集合进行排序,然后按以下代码中的字段 LoggedDateTime 对集合进行排序:
我认为将会按枚举的整数表示形式进行排序。因此,您的枚举将出现致命、错误、警告等。或者您可以使用 IComparer (来自 Yahia 答案)来定义您自己的比较规则。
You can sort the collection by the field LoggedType, then by the field LoggedDateTime in the following code:
I think there will be sorting by integer representation of enum. So with your enum there will be Fatal, Error, Warn etc. Or you can use IComparer (from Yahia answer) for your own definition rules of comparison.
你需要做类似的事情:
You need to do something like:
问题是
false
排在true
之前。因此,按照您的顺序,首先是那些非致命的。首先出现的不是错误等。
您可以使用不等式使其发挥作用:
等等
,或使用
OrderByDescending
和ThenByDescending
。但我更愿意给出枚举的顺序:
然后只使用这个值:
The problem is that
false
is ordered beforetrue
.Thus first in your order come those that are not Fatal. Out of those first come those that are not Error etc.
You can use inequalities to make it work:
etc.
or use
OrderByDescending
andThenByDescending
.But I'd prefer to give an order of the enum:
and then just use this value: