none
LINQ - Trim Data Before Submit To DB RRS feed

  • Question

  • Hi,

    I want to Trim the space of the string type data and submit to db, may i know how to do this in the class level.
    Example:

       [Column(DbType = "varchar(500) NULL")]
            public string VesselName
            {
                get;
                set;
            }
    Tuesday, January 14, 2014 9:36 AM

Answers

  • Hello,

    >>From the sample given, i can't figure out why return this._OrderName.Trim() will works? and Where to set this?

    It is my mistake. Expose the .dbml file and under XXX.designer.cs file, we can see these generated entities class by LINQ2Sql model. Find the property you want to trim as below:

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_OrderCode", DbType="NVarChar(50) NOT NULL", CanBeNull=false)]
    
                                    public string OrderCode
    
                                    {
    
                                                    get
    
                                                    {
    
                                                                    return this._OrderCode.Trim();
    
                                                    }
    
                                                    set
    
                                                    {
    
                                                                    if ((this._OrderCode != value))
    
                                                                    {
    
                                                                                    this.OnOrderCodeChanging(value);
    
                                                                                    this.SendPropertyChanging();
    
                                                                                    this._OrderCode = value;
    
                                                                                    this.SendPropertyChanged("OrderCode");
    
                                                                                    this.OnOrderCodeChanged();
    
                                                                    }
    
                                                    }
    
                                    }
    

    Regards.                               


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 15, 2014 8:10 AM
    Moderator

All replies

  • I don't know if this is helpful, but if you would do it in something like a trigger then you can read in this thread how to do that.

    http://social.msdn.microsoft.com/Forums/en-US/667cd158-cd92-46eb-8d91-aca127eabf7f/replace-a-sql-trigger-with-code?forum=linqtosql

    Tuesday, January 14, 2014 3:15 PM
  • Hello,

    >>I want to Trim the space of the string type data and submit to db, may i know how to do this in the class level.

    For what you want, we just need to modify the designer generated getters for the fields we want trimmed to call the trim method as below:

    return this._OrderName.Trim();

    My new inserted data:

      Order order = new Order() { OrderID = 5, OrderCode = "Test ", OrderName = " Test" };
    
    
                    db.Orders.InsertOnSubmit(order);
    
    
                    db.SubmitChanges();
    

    We can see that OrderName has white space in the value.

    The data after being inserted into database and I inserted another record without Trim() to do a comparison:

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 15, 2014 1:57 AM
    Moderator
  • Hi,

    I don't understand what you mean by designer generated getters.
    can you give a complete sample?
    From the sample given, i can't figure out why return this._OrderName.Trim() will works? and Where to set this?
    Wednesday, January 15, 2014 3:51 AM
  • Hello,

    >>From the sample given, i can't figure out why return this._OrderName.Trim() will works? and Where to set this?

    It is my mistake. Expose the .dbml file and under XXX.designer.cs file, we can see these generated entities class by LINQ2Sql model. Find the property you want to trim as below:

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_OrderCode", DbType="NVarChar(50) NOT NULL", CanBeNull=false)]
    
                                    public string OrderCode
    
                                    {
    
                                                    get
    
                                                    {
    
                                                                    return this._OrderCode.Trim();
    
                                                    }
    
                                                    set
    
                                                    {
    
                                                                    if ((this._OrderCode != value))
    
                                                                    {
    
                                                                                    this.OnOrderCodeChanging(value);
    
                                                                                    this.SendPropertyChanging();
    
                                                                                    this._OrderCode = value;
    
                                                                                    this.SendPropertyChanged("OrderCode");
    
                                                                                    this.OnOrderCodeChanged();
    
                                                                    }
    
                                                    }
    
                                    }
    

    Regards.                               


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 15, 2014 8:10 AM
    Moderator
  • Hi,
    Thx. i will try the solution.
    Just curious, why modify get {} , will affect the set {}, i thought what submit to db is using set {}.
    Wednesday, January 15, 2014 9:41 AM
  • Hi,

    >>Just curious, why modify get {} , will affect the set {}, i thought what submit to db is using set {}.

    When we instant a new entity as below:

    Order order = new Order() { OrderID = 6, OrderCode = "Test      ", OrderName = "       Test" };

    It actually will use its set{} to set _OrderName = “     Test” and when we call submit, it will use its get{} to get the value. Since we do a trim operation in get{}, it will return a trim value.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, January 16, 2014 9:08 AM
    Moderator