如何使用Linq to Sql修剪值? [英] How to trim values using Linq to Sql?

查看:83
本文介绍了如何使用Linq to Sql修剪值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据库中,我有一个名为Contact的表.名字和其他此类字符串字段设计为使用Char数据类型(不是我的数据库设计).我的对象Contact映射到属性中的字符串类型.如果我想做一个简单的测试,通过id检索Contact对象,我会做这样的事情:

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 contact = db.Contacts.Single(c => c.Id == myId);
Contact test = new Contact();
test.FirstName = "Martin";

Assert.AreEqual(test.FirstName, contact.FirstName);

由于char类型,contact.FirstName的值为"Martin".当FirstName属性被加载时,在哪里可以拦截?在初始加载(接触)时不会调用OnFirstNameChanging(字符串值),而在测试对象上会调用它.

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.

推荐答案

也许您可以将其放在OnLoaded()局部方法中?注意:我从没使用过,但是我认为它看起来像这样:

Maybe you could put it in the OnLoaded() partial method? Note: I've never used this, but I assume it would look like this:

public partial class Contact
{
    partial void OnLoaded()
    {
        FirstName = FirstName.Trim();
    }
}

这篇关于如何使用Linq to Sql修剪值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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