none
Extending my DBContext to convert string to date RRS feed

  • Question

  • How can I extend my DBContext class to give myself a function to convert a string in format "yyyyMMdd" to dateTime so I can .AddYears(-1) to it.

    I saw where someone gave an example for adding an .IsNumeric("123") function. this is what they showed:

    [Function(Name = "ISNUMERIC", IsComposable = true)]
    public int IsNumeric(string input)
    {
        throw new NotImplementedException(); // this won't get called 
    }

    This is what I did, but it doesn't work yet.

    Function(Name = "TODATETIME", IsComposable = true)]
    public static DateTime ToDateTime(this CalculationDetail test, string input)
    {
        throw new NotImplementedException();
    }
    Any help is appreciated.
    Thursday, May 10, 2012 8:58 PM

Answers

  • doesn't work yet

    It's best to describe what happens (for example if one tries to repro your issue, it is easier to be nearly sure that the same issue has been successfully reproduced, it could be easier to search for the error or one could understand what happens from the behavior you described or error message you gave).

    For now :
    - this is not the same pattern that then one you show. So if the first sample works, try to use first the same pattern and then only try to change to an extension method if you really want to (but keep in mind that an extension method is not really part of the type but rather just syntactic sugar so I doubt it will work)
    - Name is the name of the server side function. Are you sure you have a Transact SQL TODATETIME function ? Or is this some confusion with some client side function Convert.ToDateTime function ?

    Finally not sure why you need to convert strings to dates server side but it sounds like a bad idea. For example it could depend on server side settings (i.e. 01/02/2012 is either January 2 or February 1). Usually a good practice is to keep data under their native type as long as possible that is to convert them just before displaying them and to convert them back once they have been entered by the user (or at least to transmit them to the server under their native format).

    To start with does it work with the IsNumeric function ? If I remember it was some LINQ to SQL stuff and I'm not sure it works with Linq TO EF (would have to try but I gave a quick look and it doesn't seems to be available, do you use the "code first" model ? IMO won't work with Code First, should work if you have an EDMX file (by updating the EDMX file)). I would really double check you do need to convert strings server side...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


    Monday, May 14, 2012 5:57 PM

All replies

  • Hi DavidGerler,

    Welcome!

    We will do some more pending research  about your problem and come back as soon as possible, Thanks for understanding.

    Have a nice day.


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 14, 2012 2:14 AM
    Moderator
  • Hi DavidGerler,

    If you only want to format the string, you can write the convert method in a partial class. The classes which Entity Framework generated are partial, so you can create another partial class to contain all of the methods which you want.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 14, 2012 6:50 AM
    Moderator
  • doesn't work yet

    It's best to describe what happens (for example if one tries to repro your issue, it is easier to be nearly sure that the same issue has been successfully reproduced, it could be easier to search for the error or one could understand what happens from the behavior you described or error message you gave).

    For now :
    - this is not the same pattern that then one you show. So if the first sample works, try to use first the same pattern and then only try to change to an extension method if you really want to (but keep in mind that an extension method is not really part of the type but rather just syntactic sugar so I doubt it will work)
    - Name is the name of the server side function. Are you sure you have a Transact SQL TODATETIME function ? Or is this some confusion with some client side function Convert.ToDateTime function ?

    Finally not sure why you need to convert strings to dates server side but it sounds like a bad idea. For example it could depend on server side settings (i.e. 01/02/2012 is either January 2 or February 1). Usually a good practice is to keep data under their native type as long as possible that is to convert them just before displaying them and to convert them back once they have been entered by the user (or at least to transmit them to the server under their native format).

    To start with does it work with the IsNumeric function ? If I remember it was some LINQ to SQL stuff and I'm not sure it works with Linq TO EF (would have to try but I gave a quick look and it doesn't seems to be available, do you use the "code first" model ? IMO won't work with Code First, should work if you have an EDMX file (by updating the EDMX file)). I would really double check you do need to convert strings server side...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


    Monday, May 14, 2012 5:57 PM
  • Hi DavidGerler,

    Have you solved the issue? I look forward to hearing from you.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, May 16, 2012 2:36 AM
    Moderator
  • - Name is the name of the server side function. Are you sure you have a Transact SQL TODATETIME function ? Or is this some confusion with some client side function Convert.ToDateTime function ?

    Finally not sure why you need to convert strings to dates server side but it sounds like a bad idea. For example it could depend on server side settings (i.e. 01/02/2012 is either January 2 or February 1). Usually a good practice is to keep data under their native type as long as possible that is to convert them just before displaying them and to convert them back once they have been entered by the user (or at least to transmit them to the server under their native format).

    I think you hit what I needed with the server side function. I don't have a server side function "TODATETIME", but I do have a UDF called "DDATE" which converts the string YYYYMMDD to a date in the form of MM/DD/YYYY and perhaps I can add a server side function to handle the dateadd as well.

    The reason to convert the string server side...

    I am joining a table onto itself so that I can find out if it had sales one year ago. So I need to take the date in the left table and add a year to it to join it to the right table. If I do the conversion application side, I'll only get one date to use on the right side of the join. It needs to be a different date for each row as I'm now needing to return the sales for the same period one year ago.

    Unfortuunately, I had to go ahead and switch to Linq to SQL so I'm unable to actually test this right now. I'll definitely keep it in mind when I start my next project using Linq To Entities.

    Thank you.

    Sunday, May 20, 2012 2:16 PM