110,536
社区成员
发帖
与我相关
我的任务
分享
List<AlertLog> Alst = new List<AlertLog>();
public void WriteIntoLog(AlertLog alertLog)
{
int count = 0;
foreach (var item in this.Alst)
{
if (item.AlertCode == alertLog.AlertCode)
{
count++;
}
}
if (count < 20)
{
this.Alst.Add(AlertLog);
}
else
{
}
}
public class AlertLog : IComparable
{
public string AlertCode { get; set; }
public DateTime AlertTime { get; set; }
public int CompareTo(object obj)
{
AlertLog log = (AlertLog)obj;
if (log != null)
return this.AlertTime.CompareTo(log.AlertTime);
throw new ArgumentException();
}
}
List<AlertLog> LogList = new List<AlertLog>();
void WriteLog(AlertLog log)
{
LogList.Add(log);
List<AlertLog> temp = LogList.Where(x => x.AlertCode == log.AlertCode).ToList();
if (temp.Count > 20)
{
temp.Sort();
LogList.Remove(temp[0]);
}
}
public static void WriteIntoLog(AlertLog alertLog)
{
int count = 0;
AlertLog minDateTimeLog = alertLog;//如果要比较输入参数则minDateTimeLog=alertLog,
//如果不需要比较输入参数则minDateTimeLog = null;
foreach (var item in Alst)
{
if (item.AlertCode == alertLog.AlertCode)
{
count++;
if(minDateTimeLog == null || item.AlertTime < minDateTimeLog.AlertTime)
minDateTimeLog = item;
}
}
//Alst.Add(alertLog);如果需要比较输入参数则这个注释打开
if (count >= 20)
{
Alst.Remove(minDateTimeLog);
}
Alst.Add(alertLog);//如果不需要比较输入参数则这行注释掉
}
void WriteLog(AlertLog log)
{
LogList.Add(log);
List<AlertLog> temp = LogList.Where(x => x.AlertCode == log.AlertCode).OrderBy(o=>o.AlertTime)ToList();
if (temp.Count > 20)
{
LogList.Remove(temp[0]);
}
}
List<AlertLog> LogList = new List<AlertLog>();
void WriteLog(AlertLog log)
{
LogList.Add(log);
if (LogList.Count > 20)
{
LogList.RemoveAt(0);
}
}
List<AlertLog> LogList = new List<AlertLog>();
void WriteLog(AlertLog log)
{
LogList.Add(log);
if (LogList.Count > 20)
{
LogList.Remove(temp[0]);
}
}
这样子不行吗?新手,求教育