C# search list date/time

I have an ASP.Net page that displays a gridview from a textfile. The search is done with a list. The search works for all but one of my fields, which ios a date time field. How can I get it to search the field. I provided some of the code below. Thanks.
case "time":
  List<LogRec> timelogs = lr.FindAll(delegate(LogRecord rec) {retunr rec.Time.Equals(userText); });
   return timelogs;
case "time":
  List<LogRec> complogs = lr.FindAll(delegate(LogRecord rec) {retunr rec.Component.Equals(userText); });
   return complogs;

Open in new window

dodgerfanAsked:
Who is Participating?
 
Todd GerbertConnect With a Mentor IT ConsultantCommented:
Well, cut & paste is a wonderful thing. ;)  Also, you could just put a .txt on the end of the filename and attach it.

You definitely need to convert userText into a DateTime object in order to compare it with another DateTime object, use DateTime.Parse(userText).

You can see that "2/3/2009 12:00:00 AM" is definitely not equal to "2/3/2009 1:33:42 PM", therefore your FindAll *should* return an empty set.

Your problem appears to be how you're collecting the value of userText, not with your search function.  Are you getting userText with a single text box, or are you using a DateTimePicker?  Using separate text boxes for the time and date?  Somewhere between where the user enters the date, but before the call to SearchList, is it being converted into a DateTime object and back into a string?
0
 
Todd GerbertIT ConsultantCommented:
Are you aware you've mis-spelled return in the code-blocks?  Hopefully so...shouldn't run at all with the code that way!

Also, try parsing the the users' text into a DateTime object, i.e.

lr.FindAll(delegate(LogRecord rec) {return rec.Time.Equals(DateTime.Parse(userText)); });

Of course, that'll throw an exception if the text isn't a valid date/time value.

0
 
dodgerfanAuthor Commented:
I hva etried parsing the user text into a DateTime object, just as you suggested. It does not seem to work. And I ensure I'm entering the data properly. I'm not sure why it will not work. Any thoughts?
Return is spelled correctly in the real code. This was re-typed quickly, not cut and pasted. Different networks. Thanks.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Todd GerbertIT ConsultantCommented:
I think your function needs to take a string parameter.

I'll take a look in minute...
0
 
Todd GerbertIT ConsultantCommented:
Your FindAll function needs to return a bool, and take a single parameter that is of the same type as contained in the List<T>, so assuming lr is a List<LogRec> then:

List<LogRec> timelogs = lr.FindAll(delegate(LogRec rec) {return rec.Time.Equals(userText); });

...or if lr is a List<LogRecord> then:

List<LogRecord> timelogs = lr.FindAll(delegate(LogRecord rec) {return rec.Time.Equals(userText); });

Also, I don't know the details of your LogRec/LogRecord class, so if rec.Time is DateTime you should need to parse it into DateTime, however if it's a string you may want to make sure you're trimming whitespace, i.e. userText.Trim().

Below is an example of how you might go about it using DateTime's.  I used a separate function (rather than writing an anonymous method inline as you have) because I've expanded the comparison routing slightly (to account for invalid user input), and you look like you're using it in several places in your code; easier to write the lengthy piece once and pass it as a delegate to FindAll (will also reduce coding errors).  But, if you wanted to write it inline:

List<LogRec> timelogs = lr.FindAll(delegate(LogRec rec) { return rec.Time.Equals(DateTime.Parse(userText).Time); } );
	static class Program
	{
 
		private static string userTextEntry;
		
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
 
			List<DateTime> sourceList = new List<DateTime>();
			List<DateTime> resultsList;
 
			sourceList.Add(DateTime.Now.AddDays(-1));
			sourceList.Add(DateTime.Now);
			sourceList.Add(DateTime.Now.AddDays(1));
 
			userTextEntry = Microsoft.VisualBasic.Interaction.InputBox("Enter a date:", "", "", -1, -1);
 
			resultsList = sourceList.FindAll(EqualsUserEntry);
 
			MessageBox.Show(resultsList.Count.ToString() + " results found.");
 
			foreach (DateTime result in resultsList)
				MessageBox.Show(result.Date.ToString());
						
		}
 
		private static bool EqualsUserEntry(DateTime date)
		{
			DateTime usersDate;
			
			if (!DateTime.TryParse(userTextEntry, out usersDate))
				return false;
 
			return (usersDate.Date == date.Date);
		}
	}

Open in new window

0
 
dodgerfanAuthor Commented:
Sorry, I did not get back to this until after the weekend. Thanks for the help. I have not been able to get things working yet. Your inline function did not work: List<LogRec> timelogs = lr.FindAll(delegate(LogRec rec) { return rec.Time.Equals(DateTime.Parse(userText).Time); } );
I have been unable to get anything going yet. This is my first exposure to delegates, and I'm still learning C#. I've inlcuded the complete function here. Everythig works except The date time search. The user will enter a date (format for dates is 2/3/2009 1:33:43 PM). The data is from a log file, so the date and time is important. If younhave any more insight, I;d appreciate it. Thanks.

Public List<LogRecord> SearchList(List<LogRecord< lr, string userText)
{
    switch (ddlField.SelectedItem.Text)
    {
     case "Time":
     List<LogRecord> timelogs = lr.FindAll(delegate(LogRecord rec) {return rec.Time.Equals(userText); });
     return timelogs;
     case "Comp":
     List<LogRecord> omplogs = lr.FindAll(delegate(LogRecord rec) {return rec.Comp.Equals(userText); });
     return complogs;
     case "Time":
     List<LogRecord> msglogs = lr.FindAll(delegate(LogRecord rec) {return rec.Msg.Equals(int.Parse(userText)); });
     return msglogs;
}
return lr;
}

0
 
Todd GerbertIT ConsultantCommented:
It's not working meaning an exception is being thrown, or SearchList() returns an empty list/null?
0
 
dodgerfanAuthor Commented:
SearchList returns an empty list. It does not throw any errors.
0
 
Todd GerbertIT ConsultantCommented:
What I suspect is happening is that LogRecord.Time is a DateTime object, and your FindAll() function is trying to compare a DateTime object to a String object, and consequently it will always return false.  You need to compare apples to apples; convert the users' text into a DateTime object, using the static Parse functions of the DateTime class.

What's below will compare all pieces of the date/time information, therefore if the user enters "02/23/2009" it will NOT match a DateTime whose value is "02/23/2009 11:48:32 AM"


public List<LogRecord> SearchList(List<LogRecord> lr, string userText)
{
	switch (ddlField.SelectedItem.Text.Trim().ToLower())
	{
		case "time":
			List<LogRecord> timelogs = lr.FindAll(delegate(LogRecord rec) { return rec.Time.Equals(DateTime.Parse(userText)); });
			return timelogs;
		case "comp":
			List<LogRecord> omplogs = lr.FindAll(delegate(LogRecord rec) {return rec.Comp.Equals(userText); });
			return complogs;
		case "messagelogs":
			List<LogRecord> msglogs = lr.FindAll(delegate(LogRecord rec) {return rec.Msg.Equals(int.Parse(userText)); });
			return msglogs;
	}
	return lr;
}

Open in new window

0
 
dodgerfanAuthor Commented:
Well, I've tried the suggestion. I had tried the DateTime.Parse(userText) already. It returns blank page. I cut and paste the date into the search field so I get the correct data and format. Once I put the Trim() in, the search stopped working for all of the fields. No erros, it just does not do anything. Does this sound like anything you;ve seen before? Thanks again for the help.
0
 
Todd GerbertIT ConsultantCommented:
Put a breakpoint on the first line below "case "time":"  Use F11 to step-into the FindAll function, and hover the mouse over "rec" - you should be able to browse it's members - what is the type and value of rec.Time?  Hover the mouse over userText, what is it's value?

Post your whole code if you want, it's hard to say just seeing small pieces.
0
 
dodgerfanAuthor Commented:
Ok, the value of userText is "2/3/2009 12:00:00 AM". What I entered in the textbox was 2/3/2009 1:33:43 PM. rec.Time value is {2/3/2009 1:33:43 PM}. Type looks like date. There's no way to post the code without me re-typing it, and it's gotten pretty large. It is all in code-behind page.
0
 
dodgerfanAuthor Commented:
I am using a single text box to get the userText. All one textbox. The datetime is being converted into a string when the page opens up and displays the gridview.
time= DateTime.Parse(String.Format("{0} {1}", GetFieldValue(fields, "date"), GetFieldValue(fields, "time")));
The dev network this app is on is separte from the network that allows internet access. I can't cut and paste. I can;t even print it out and re-type it.
0
All Courses

From novice to tech pro — start learning today.