问题映射/铸造的LINQ到SQL不同类型的 [英] Problem with Mapping/Casting Linq-to-Sql on different Types

查看:194
本文介绍了问题映射/铸造的LINQ到SQL不同类型的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许有人可以提供帮助。

maybe someone can help.

我想有映射的LINQ类不同的数据类型。

I want to have on mapped Linq-Class different Datatype.

这是工作:

 private System.Nullable<short> _deleted = 1;

 [Column(Storage = "_deleted", Name = "deleted", DbType = "SmallInt", CanBeNull = true)]
    public System.Nullable<short> deleted
    {
        get
        {
            return this._deleted;
        }
        set
        {
            this._deleted = value;
        }
    }



当然可以。但是,没有,当我需要将某些逻辑布尔,像这样的:

Sure thing. But no when i want to place some logic for boolean, like this:

 private System.Nullable<short> _deleted = 1;

 [Column(Storage = "_deleted", Name = "deleted", DbType = "SmallInt", CanBeNull = true)]
    public bool deleted
    {
        get
        {
            if (this._deleted == 1)
            {
                return true;
            }
            return false;
        }
        set
        {
    if(value == true)

    {
                this._deleted = (short)1;
    }else
    {   
                this._deleted = (short)0;
    }
        }
    }



我总是得到运行时错误:

I get always runtime error:

[TypeLoadException: GenericArguments[2], "System.Nullable`1[System.Int16]", on 'System.Data.Linq.Mapping.PropertyAccessor+Accessor`3[T,V,V2]' violates the constraint of type parameter 'V2'.]

我不能改变数据库位..我需要在映射类铸件。

I can't change the database to bit.. I need to have casting in mapping class.

**的更新

据mmcteam,在未映射的方法铸造的伎俩。
不是很确定如果是彪要使用的OR映射的方式,但它的作品。

According to mmcteam, casting in unmapped method does the trick. Not so sure if it is ment to be using the OR Mapping that way, but it works.

    private System.Nullable<short> _deleted = 1;

    public bool deleted
    {
        get
        {
            if (this._deleted == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        set
        {
            if (value)
            {
                this._deleted = 1;
            }
            else
            {
                this._deleted = 0;
            }
        }
    }


    [Column(Storage = "_deleted", Name = "deleted", DbType = "SmallInt", CanBeNull = true)]
    private short? deleted_smallint
    {
        get
        {
            return this._deleted;
        }
        set
        {
            this._deleted = value;
        }
    }



** 通知/问题

您不能使用在LINQ查询没有映射方法!

You can't use the not mapped methods on linq queries!

                 var result = from p in dc.Products
                               where p.enabled && !p.deleted 
                select p;



将导致不支持SQL异常。如果没有where条件,数据出来用正确的值。

causes not supported sql exception. Without the where condition, data comes out with correct values.

推荐答案

或者只是多了一个属性添加到您的行类,并投上一一个布尔。

Or just add one more property to your row class and cast previous one to bool.

这篇关于问题映射/铸造的LINQ到SQL不同类型的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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