问题是这样的,c#中读取一个日志文件,里面有日期和错误名称,现在添加了一个搜索条件根据开始日期和结束日期进行查询,或其它条件,现在是时间条件不知道怎么添加,望高手指教,谢谢。
 
   public static List<LogModel> GetLogs(string path)
        {
            List<LogModel> list = new List<LogModel>();
            string result = string.Empty;
            string single = string.Empty;
            FileStream fs = new FileStream(path,FileMode.Open);
            StreamReader sr = new StreamReader(fs,Encoding.Default);
            result = sr.ReadToEnd();
            sr.Close();
            sr.Dispose();
            fs.Close();
            fs.Dispose();
            Regex reg_Fore = new Regex(@"(\d{4}-\d{1,2}-\d{1,2}.*)",RegexOptions.IgnoreCase);
            Regex reg_item = new Regex(@"(?<time>\d{4}-\d{1,2}-\d{1,2} \d{2}\:\d{2}:\d{2},\d+)(  | )(?<level>[^ ]*) \[(?<type>[^\]]*)\] \[(?<theadid>\d[\]]*)\] -  \[(?<fun>[^\]]*)\] \[(?<info>[^\]]*)\]", RegexOptions.IgnoreCase);
            MatchCollection mc = reg_Fore.Matches(result);
            int a = mc.Count;
            Match match;
            foreach (Match m in mc)
            {
                LogModel lm = new LogModel();
                single = m.Groups[1].Value;
                match = reg_item.Match(single);
                if (match.Success)
                {
                    lm.Data =MyUnity.GetStrToDatatime(match.Groups["time"].Value, DateTime.Now);
                    lm.Level = match.Groups["level"].Value;
                    lm.LogNumber=match.Groups["type"].Value;
                    lm.TheadID = match.Groups["theadid"].Value;
                    lm.Function = match.Groups["fun"].Value;
                    lm.Info = match.Groups["info"].Value;
                    list.Add(lm);
                }
            }
            return list;
        }
  private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            List<LogModel> list = GetLog.GetLogs(filePath);
            IEnumerable<LogModel> results = list.Where(LogModel => LogModel.Data.ToString("MM/dd/yyyy") == dpBegin.Text);//写到到不知道怎么添加条件了。
            DataGrid_Log.ItemsSource = results;
        } 

解决方案 »

  1.   

     list.Where((LogModel) =>{ 
    int year=LogModel.Data.Year;
    int month=LogModel.Data.Month;
    int day=LogModel.Data.Day;
    if(条件)
    retrun true;
    else
    retrun false;
    }
    );
      

  2.   

       解决了,最近总把简单问题复杂话
       
    private void btnSearch_Click(object sender, RoutedEventArgs e)
            {
                List<LogModel> list = GetLog.GetLogs(filePath);
                IEnumerable<LogModel> results = list;
                string conditon = string.Empty;
                if (cbLevel.Text != "请选择")
                    results = results.Where(LogModel => (LogModel.Level == cbLevel.Text));
                if (!string.IsNullOrEmpty(dpBegin.Text))
                    results = results.Where(LogModel => (LogModel.Data >= Convert.ToDateTime(dpBegin.Text)));
                if (!string.IsNullOrEmpty(dpEnd.Text))
                    results = results.Where(LogModel => (LogModel.Data <= Convert.ToDateTime(dpEnd.Text).AddDays(1D)));
                DataGrid_Log.ItemsSource = results;
            }