CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2012
    Posts
    2

    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".

  2. #2
    Join Date
    Dec 2008
    Posts
    144

    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
    Code:
    if (Issue.Resolved)
    {
         ThreadTools.Click();
         MarkThreadResolved();
    }

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Trying to find date of 2010 for all records in a list (version 4.0)

    Secondly... .Equals is a method, not a property...
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  4. #4
    Join Date
    Mar 2012
    Posts
    2

    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'

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    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".
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  6. #6
    Join Date
    Dec 2008
    Posts
    144

    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.
    Code:
    if (Issue.Resolved)
    {
         ThreadTools.Click();
         MarkThreadResolved();
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured