C# 在不同类型上映射/转换Linq到Sql时出现问题

C# 在不同类型上映射/转换Linq到Sql时出现问题,c#,linq,linq-to-sql,casting,mapping,C#,Linq,Linq To Sql,Casting,Mapping,也许有人能帮忙 我希望在映射的Linq类上有不同的数据类型 这是有效的: private System.Nullable<short> _deleted = 1; [Column(Storage = "_deleted", Name = "deleted", DbType = "SmallInt", CanBeNull = true)] public System.Nullable<short> deleted { get

也许有人能帮忙

我希望在映射的Linq类上有不同的数据类型

这是有效的:

 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;
        }
    }
我无法将数据库更改为bit。。我需要在映射类中进行转换

**更新

根据mmcteam的说法,在未映射的方法中进行强制转换可以达到目的。 不太确定是否以这种方式使用OR映射,但它是有效的

    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;
        }
    }
导致不支持的sql异常。如果没有where条件,数据将以正确的值显示。

是否希望这样:

[Column(Storage = "_deleted", Name = "deleted", DbType = "Bit", CanBeNull = false)] 
public bool deleted ...
与此相反:

[Column(Storage = "_deleted", Name = "deleted", DbType = "SmallInt", CanBeNull = true)] 
public bool deleted ...

或者只需在row类中添加一个属性,并将前一个属性强制转换为bool。

否,因为这也不起作用。dbtype不是Boleanen,是不可更改的SmallInt!这只会导致另一个错误数据类型布尔的运行时错误。好的,我们刚刚解决了下一个问题。无法在linq查询中使用未映射的方法。请参阅有问题的更新。在linq查询中使用自定义属性不是一个好主意,假设您想按该新属性分组或排序。Linq无法为您生成该查询。但根据具体情况,您可以在linq上编写所需的所有内容,然后执行查询(例如调用ToList()),然后处理内存中的对象。您可以在LinqToObject的查询中使用任何cutom方法,但随后强制转换方法一开始并不能解决我的问题。ToList()不是一个合适的解决方案,因为sql选择次数增加了x倍,内存/性能问题也增加了x倍。我知道,这取决于使用情况。正如我看到的唯一的方式-编辑DB结构(或者在所有查询中与您的属性相同的演员,但不是EF允许的所有演员),您可以考虑使用EFExtEngEngult来构建自定义SQL,但这是我最后要做的事情。
                 var result = from p in dc.Products
                               where p.enabled && !p.deleted 
                select p;
[Column(Storage = "_deleted", Name = "deleted", DbType = "Bit", CanBeNull = false)] 
public bool deleted ...
[Column(Storage = "_deleted", Name = "deleted", DbType = "SmallInt", CanBeNull = true)] 
public bool deleted ...