LINQ在字符串上放置不需要的尾随空格 [英] LINQ puts unwanted trailing spaces on strings

查看:109
本文介绍了LINQ在字符串上放置不需要的尾随空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对LINQ和MS SQL Server有一个非常烦人的问题.

I have a very annoying problem with LINQ and MS SQL Server.

我目前正在使用WPF C#制作一些软件,并使用LINQ从数据库中的数据生成类.

Im currently making some software in WPF C# and use LINQ to generate classes from the data in the database.

但是,当我从程序中更新数据库中的nvarchar或varchar字段时,LINQ会在字符串中添加尾随空格!

However, when i update a nvarchar or varchar field in the DB from my program, LINQ adds trailing spaces to the string!

我在这样定义的表中有一个字段:

I have a field in a table defined like this:

ProductName = NVARCHAR(10)

所以,如果我这样做:

Product.Name = "Bike";
Product.Name = Product.Name.Trim();
repository.Add(Product);   // Runs an .InsertOnSubmit
repository.Save();         // Runs a .SubmitChanges

然后,数据库中的结果数据为"Bike [SPACE] [SPACE] [SPACE] [SPACE] [SPACE] [SPACE]",其中[SPACE]只是一个空格(在此文本中不能写多个空格在这里).

then the resulting data in the DB is "Bike[SPACE][SPACE][SPACE][SPACE][SPACE][SPACE]" where [SPACE] is just a space (can't write multiple spaces in this text here).

为什么要这样做,如何使它停止添加那些烦人的尾随空格?

Why does it do this, and how do i make it stop adding those annoying trailing spaces?

预先感谢

推荐答案

自动生成的LINQ类通过以下方式定义了NVarchar字段:

The auto-generated LINQ classes defined the NVarchar fields in the following way:

[Column(Storage="_Text", DbType="NChar(20) NOT NULL", CanBeNull=false)]
public string Text
{
get
{
    return this._Text;
}
set
{
    if ((this._Text != value))
    {
        this.OnTextChanging(value);
        this.SendPropertyChanging();
        this._Text = value;
        this.SendPropertyChanged("Text");
        this.OnTextChanged();
    }
}
}

列"设置应位于的位置:

Where the Column settings should be:

[Column(Storage="_Text", DbType="NVarChar(20) NOT NULL", CanBeNull=false)]

发生问题是因为我在不重新生成LINQ类的情况下更新了SQL数据库中的类型.

The problem occurred because i updated the type in the SQL database without re-generating the LINQ classes.

这篇关于LINQ在字符串上放置不需要的尾随空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆