Trying to find date of 2010 for all records in a list (version 4.0)
I'm going thru a list and trying to find only the records that have 2010 as their year and displaying the the record using this code:
var dateSorted =
from e in deserializedRoster
where DateTime.Equals = "2010"
select e;
foreach (var e in dateSorted) Console.WriteLine("Date 2010: {0}", e.ToString());
Console.WriteLine();
The line DateTime.Equals needs work. I don't know how to instruct the code to look only for 2010 in the year. The field is called "date".
Re: Trying to find date of 2010 for all records in a list (version 4.0)
The big problem is your WHERE clause. If the field is named date, then it needs to be:
Code:
var dateSorted = from e in deserializedRoster
where e.date.Year = "2010"
select e
You have to do your where on the temp variable you assign in your from statement. I'm assuming that the "date" field is a DateTime type, which is why I'm using the Year parameter to check.
HTH
Re: Trying to find date of 2010 for all records in a list (version 4.0)
Secondly... .Equals is a method, not a property...
Re: Trying to find date of 2010 for all records in a list (version 4.0)
I changed the code to reflect what you indicated and I'm getting two errors:
"Property or indexer 'System.DateTime.Year' cannot be assigned to -- it is ready only"
And:
"Cannot implicitly convert type 'string' to 'int'
Re: Trying to find date of 2010 for all records in a list (version 4.0)
He left of a '=' The equality operator is ==, not =. Also, DateTime.Year is an int, so remove the quotes around "2010".
Re: Trying to find date of 2010 for all records in a list (version 4.0)
Yeah, I'm always forgetting the second = for equality operators.