C# 如何使用 Linq to Sql 修剪值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/651982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to trim values using Linq to Sql?
提问by Marsharks
In the database, I have a table called Contact. The first name and other such string fields are designed to use a Char datatype (not my database design). My object Contact maps to a string type in the properties. If I wanted to do a simple test retrieving a Contact object by id, I would do something like this:
在数据库中,我有一个名为 Contact 的表。名字和其他此类字符串字段旨在使用 Char 数据类型(不是我的数据库设计)。我的对象 Contact 映射到属性中的字符串类型。如果我想做一个通过 id 检索 Contact 对象的简单测试,我会这样做:
Contact contact = db.Contacts.Single(c => c.Id == myId);
Contact test = new Contact();
test.FirstName = "Martin";
Assert.AreEqual(test.FirstName, contact.FirstName);
The contact.FirstName value is "Martin " because of the char type. Where can I intercept the FirstName property when it is being loaded? OnFirstNameChanging(string value) does not get called on the initial load (contact), but does on the test object.
由于字符类型,contact.FirstName 值为“Martin”。加载 FirstName 属性时,在哪里可以拦截它?OnFirstNameChanging(string value) 不会在初始加载(接触)时被调用,但会在测试对象上被调用。
采纳答案by Chris Shaffer
Maybe you could put it in the OnLoaded() partial method? Note: I've never used this, but I assume it would look like this:
也许你可以把它放在 OnLoaded() 部分方法中?注意:我从未使用过它,但我认为它看起来像这样:
public partial class Contact
{
partial void OnLoaded()
{
FirstName = FirstName.Trim();
}
}
回答by Richard
Contact contact = db.Contacts.Single(c => c.Id.Trim() == myId);
And check that the LINQ provider translates that into the appropriate SQL.
并检查 LINQ 提供程序是否将其转换为适当的 SQL。
回答by tvanfosson
If you can't change the schema, you might want to make the designer generated accessor private/protected and create a public accessor to front-end the property in a partial class implementation. You can then Trim the value in the get accessor.
如果您无法更改架构,您可能希望将设计器生成的访问器设为私有/受保护,并创建一个公共访问器以在部分类实现中对属性进行前端处理。然后,您可以修剪 get 访问器中的值。
public partial class Contact
{
public string RealFirstName
{
get { return this.FirstName.Trim(); }
set { this.FirstName = value; }
}
...
}
回答by Samuel
You can run a ForEach
on your list of contacts before trying to search for Martin.
ForEach
在尝试搜索 Martin 之前,您可以在联系人列表上运行 a 。
var contacts = db.Contacts.ForEach(c => c.FirstName = c.FirstName.Trim());
Contact contact = contacts.Single(c => c.Id == myId);
contact.FirstName // = "Martin"
But this way is not very easy to maintain if it has to be done for multiple fields.
但是这种方式如果要针对多个字段做的话,就不太容易维护了。
回答by Roger Willcocks
It depends on the control you have over the schema and the code.
这取决于您对架构和代码的控制。
If the values are being set up by calling a constructor with all parameters, do the trims, etc as they are assigned to the member variables.
如果值是通过调用带有所有参数的构造函数来设置的,请在将它们分配给成员变量时进行修剪等。
If they are being assigned to the properties (probably from the example), change the SET accessor so that it does the trim there.
如果它们被分配给属性(可能来自示例),请更改 SET 访问器,以便它在那里进行修剪。
You do have potential issues if you actually WANT leading or trailing spaces at some point though.
但是,如果您确实想要在某个时候使用前导或尾随空格,则确实存在潜在问题。
If you can't modify the base class code, try using partial classes, or inheriting from the class and overriding the properties there.
如果您无法修改基类代码,请尝试使用部分类,或从类继承并覆盖那里的属性。
If you can't do that, then my last suggestion would be to write a factory class of some sort that you pass a created object to and it cleans it up per the rules you want.
如果您不能这样做,那么我的最后一个建议是编写某种工厂类,您将创建的对象传递给它,并根据您想要的规则对其进行清理。