Solved

filter a list by date

Posted on 2015-02-19
3
99 Views
Last Modified: 2015-02-19
I have  a list that currently has data it is called mylog
I want to filter on that list by 2 datetime fields from and to. I have 2 datetimepickers for from and to - I want the user to be able to select dates and filter the current list and display only that data
The field in the list to be filtered is

DateTime myt = mylog.ExpandedSurveyData[i].OriginalSurveyData.SurveyTime;

Open in new window

I tried this just to see what would happen but its wrong
var query = mylog.ExpandedSurveyData[0].OriginalSurveyData.SurveyTime.Where(l => l.DateValue.IsBetween(start, End));

Open in new window

0
Comment
Question by:r3nder
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40619916
Quick and dirty example:
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q28620540
{
	class Program
	{
		static List<LogEntry> logEntries = new List<LogEntry>();

		static void Main(string[] args)
		{
			for (int i = 0; i < 100; i++)
				logEntries.Add(new LogEntry() { Start = DateTime.Now.AddDays(i), End = DateTime.Now.AddDays(i + 1).AddHours(i), Message = string.Format("Log Entry {0}", i), Severity = (Severity)(i % 5) });

			DateTime start = DateTime.Now.AddDays(30);
			DateTime end = DateTime.Now.AddDays(50);
			var query = (from entry in logEntries where (entry.Start >= start) && (entry.End <= end) select entry);
			foreach (var entry in query)
				Console.WriteLine(entry);

			Console.ReadLine();
		}
	}

	class LogEntry
	{
		public DateTime Start { get; set; }
		public DateTime End { get; set; }
		public string Message { get; set; }
		public Severity Severity { get; set; }

		public override string ToString()
		{
			return string.Format("{0}: {1}; {2} - {3}", Severity, Message, Start.ToString("MM/dd/yyyy"), End.ToString("MM/dd/yyyy"));
		}
	}

	enum Severity
	{
		Debug = 0,
		Information = 1,
		Warning = 2,
		Error = 3,
		Fatal = 4
	}
}

Open in new window

Produces the following output -Capture.JPG-saige-
0
 
LVL 6

Author Comment

by:r3nder
ID: 40620034
that does it for me thanks saige
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 40620035
Thanks
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is about my first experience with programming Arduino.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question