locked
LINQ DateTime .ToShortDateString RRS feed

  • Question

  • I am attempting to pull from the database and combine two datetime fields together in order to display in a DataGrid.  This works fine if I do this method:

     

    ReinsDate = r.ReinsuranceAgreementStartDate + test + r.ReinsuranceAgreementEndDate

     

    This does bring back the data fine, but it also has the long datetime on it with hours, minutes and seconds.  I am trying to get them removed and display them in the grid.  The LINQ does not appear to like the .ToShortDateString

     

    When I have this code in:

    ReinsDate = r.ReinsuranceAgreementStartDate.ToShortDateString() + test + r.ReinsuranceAgreementEndDate.ToShortDateString(),

     

    I recieve the following error:

    Could not translate expression 'r.ReinsuranceAgreementEndDate.ToShortDateString()' into SQL and could not treat it as a local expression.

     

    I am not sure what to do at this point.  I have tried .Concat and various other methods and all get similar errors.

     

    I am hoping that the ending result looks something like 4/22/2008 - 4/22/2050.

     

    Thanks in advance.

    Thursday, April 24, 2008 7:21 PM

Answers

  • Another option would be to put a format specifier on the column in the data grid.

     

    [)amien

     

    Friday, May 30, 2008 6:22 PM

All replies

  • One option is to change the query like so:

     

     

    Code Snippet

      

      IEnumerable<string> q =

     

           (from r in ...
             select new
             {
                 x1 = r.ReinsuranceAgreementStartDate.ToShortDateString(),
                 x2 = test,
                 x3 = r.ReinsuranceAgreementEndDate.ToShortDateString()
             }

            ).AsEnumerable().Select(o => o.x1 + o.x2 + o.x3);

     

     


    Hope that helps,

     

    --Samir

     

    Thursday, April 24, 2008 10:24 PM
  • Thanks for the help, but this is still not working.  I also have more fields in the LINQ query that I am trying to pull out so I am not sure if this method will work for me or not.  I put in the code suggested above and here is the error that I am getting:

     

    The non-generic type 'System.Collections.IEnumerable' cannot be used with type arguments 

     

    I tried looking this up, but not much help out there that I could use.

     

    If I have other fields in the database table that I want to use, how do I use the above code.

     

    Thanks

    Pat

    Friday, April 25, 2008 1:04 PM
  • I think the reason you're getting this error is because you're trying to call methods within IQueryable objects. Keep in mind that IQueryable extensions, unlike IEnumerable extensions, attempt to convert your lambda expressions to SQL. Naturally, methods that aren't handled by the IQueryable extensions cannot be converted, which in turn generates the error.

     

    A solution would be to call the AsEnumerable method on the primary source table:

     

    Code Snippet

    IEnumerable<string> dates = from r in db.Reinsurances.AsEnumerable()

    select r.ReinsuranceAgreementStartDate.ToShortDateString() +

    test +

    r.ReinsuranceAgreementEndDate.ToShortDateString();

     

     

    Saturday, April 26, 2008 6:58 AM
  • Another option would be to put a format specifier on the column in the data grid.

     

    [)amien

     

    Friday, May 30, 2008 6:22 PM
  •  

    another option could be that it would be fixed??
    Thursday, September 25, 2008 12:29 PM