Linq partial method execution

Last post 11-08-2007 3:35 AM by alek.lynge. 2 replies.

Sort Posts:

  • Linq partial method execution

    11-07-2007, 3:28 AM
    • Loading...
    • alek.lynge
    • Joined on 11-07-2007, 3:13 AM
    • Posts 2

    Hi.
    I'm trying to strip the date part from a DateTime object and store it with a generical date (1900/1/1) in the database field 'day_start' (datetime). I've used the ORM designer in VS 2008 to make the linq to sql bindings and it has generated the default: Dataclasses.designer.cs c# code for me. There I create a new partial method which fires whenever day_start changes, but it seems that changes to the method parameter 'value' don't get save in the following set method of the Property 'day_start'.

    Essentialy I have the following method in the Dataclasses.designer.cs:

    partial void Onday_startChanging(DateTime? value)
        {
            if (!value.HasValue)
                return;
            DateTime newval = new DateTime(1900,1,1);
            TimeSpan ts = new TimeSpan(value.Value.Hour, value.Value.Minute, 0);
            newval.Add(ts);
            value = newval;
            System.Web.HttpContext.Current.Trace.Write("time: " + value.Value.ToShortDateString() + " " + value.Value.ToShortTimeString());
        }

     Which gets fired. But value doesn't get saved with its new value (1900/1/1 + whatever hour is contained in the DateTime object).

    What am I doing wrong?

    Best Regards

    Alek

  • Re: Linq partial method execution

    11-07-2007, 11:45 AM
    • Loading...
    • vcsjones
    • Joined on 04-18-2006, 8:53 PM
    • Falls Church, VA
    • Posts 4,010
    • Moderator
      TrustedFriends-MVPs

    You are using DateTime.Add which returns a new date time, it doesn't "Add" it the the instance. So this line:

    newval.Add(ts);

    Should be:

    newval = newval.Add(ts);

    Cheers,
           Kevin Jones


  • Re: Linq partial method execution

    11-08-2007, 3:35 AM
    • Loading...
    • alek.lynge
    • Joined on 11-07-2007, 3:13 AM
    • Posts 2

    Hi.

    I did change the code so it looks like the following:
     

    if (!value.HasValue) 
                return; 
    DateTime newval = new DateTime(1900, 1, 1); 
    TimeSpan ts = new TimeSpan(value.Value.Hour, value.Value.Minute, 0); 
    value = newval.Add(ts);
     But although the value of 'value' is changed now, that doesn't get propaginated to the database itself?
Page 1 of 1 (3 items)
Microsoft Communities
Page view counter