标签: nullable

如何使视图列NOT NULL

我正在尝试创建一个视图,我希望列只有真或假.但是,似乎无论我做什么,SQL Server(2008)都认为我的位列可能会以某种方式为空.

我有一个名为"Product"的表,其中包含"Status"列INT, NULL.在视图中,我想为Product中的每一行返回一行,如果Product.Status列等于3,则将BIT列设置为true,否则位字段应为false.

示例SQL

SELECT CAST( CASE ISNULL(Status, 0)  
               WHEN 3 THEN 1  
               ELSE 0  
             END AS bit) AS HasStatus  
FROM dbo.Product  
Run Code Online (Sandbox Code Playgroud)

如果我将此查询保存为视图并查看对象资源管理器中的列,则将HasStatus列设置为BIT, NULL.但它永远不应该是NULL.我可以使用一些神奇的SQL技巧来强制使用此列NOT NULL.

请注意,如果我删除了CAST()周围的CASE列,则列被正确设置为NOT NULL,但是列的类型设置为INT,这不是我想要的.我想要它BIT.:-)

sql nullable sql-server-2008 sql-view

77
推荐指数
2
解决办法
5万
查看次数

通过反射查找可空属性的类型

我通过反射检查对象的属性,并继续处理每个属性的数据类型.这是我的(减少的)来源:

private void ExamineObject(object o)
{
  Type type = default(Type);
  Type propertyType = default(Type);
  PropertyInfo[] propertyInfo = null;

  type = o.GetType();

  propertyInfo = type.GetProperties(BindingFlags.GetProperty |
                                    BindingFlags.Public |
                                    BindingFlags.NonPublic |
                                    BindingFlags.Instance);
  // Loop over all properties
  for (int propertyInfoIndex = 0; propertyInfoIndex <= propertyInfo.Length - 1; propertyInfoIndex++)
  {
    propertyType = propertyInfo[propertyInfoIndex].PropertyType;
  }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,我新近需要处理可空属性,但我不知道如何获得可空属性的类型.

.net c# reflection nullable

76
推荐指数
4
解决办法
4万
查看次数

从'System.Int32'到'System.Nullable`1 [[System.Int32,mscorlib]]的无效演员表

Type t = typeof(int?); //will get this dynamically
object val = 5; //will get this dynamically
object nVal = Convert.ChangeType(val, t);//getting exception here
Run Code Online (Sandbox Code Playgroud)

我在上面的代码中得到了InvalidCastException.对于上面我可以简单地写int? nVal = val,但上面的代码是动态执行.

我得到一个包含在一个对象(这里是val)中的值(非可空类型,如int,float等),我必须通过将它转换为另一个类型(可以是或不可以为空的版本)将其保存到另一个对象它).什么时候

从'System.Int32'到'System.Nullable`1 [[System.Int32,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]'的无效演员表.

一个int,应该是可兑换的/可浇铸的nullable int,这里有什么问题?

.net c# casting nullable type-conversion

76
推荐指数
2
解决办法
6万
查看次数

为什么我不能检查'DateTime'是否'没什么'?

在VB.NET中,有没有办法将DateTime变量设置为"未设置"?为什么它可以设置DateTimeNothing,但没有能够检查它是否是Nothing?例如:

Dim d As DateTime = Nothing
Dim boolNotSet As Boolean = d Is Nothing 
Run Code Online (Sandbox Code Playgroud)

第二个语句抛出此错误:

'Is' operator does not accept operands of type 'Date'. Operands must be reference or
nullable types.
Run Code Online (Sandbox Code Playgroud)

vb.net null datetime nullable nothing

75
推荐指数
3
解决办法
9万
查看次数

'int?'之间有什么区别?和C#中的'int'?

我90%肯定我之前在stackoverflow上看到了这个答案,实际上我从未见过"int?" 在这里看到它之前的语法,但无论我如何搜索我找不到上一篇文章,它让我发疯.

我可能偶然吃了有趣的蘑菇,但如果我不是,有人可以指出以前的帖子,如果他们能找到它或重新解释它吗?我的stackoverflow搜索功能显然太低了....

c# syntax types nullable

74
推荐指数
4
解决办法
9万
查看次数

.NET - 将通用集合转换为DataTable

我试图将通用集合(List)转换为DataTable.我找到了以下代码来帮助我这样做:

// Sorry about indentation
public class CollectionHelper
{
private CollectionHelper()
{
}

// this is the method I have been using
public static DataTable ConvertTo<T>(IList<T> list)
{
    DataTable table = CreateTable<T>();
    Type entityType = typeof(T);
    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

    foreach (T item in list)
    {
        DataRow row = table.NewRow();

        foreach (PropertyDescriptor prop in properties)
        {
            row[prop.Name] = prop.GetValue(item);
        }

        table.Rows.Add(row);
    }

    return table;
}    

public static DataTable CreateTable<T>()
{
    Type entityType = typeof(T);
    DataTable table = new DataTable(entityType.Name);
    PropertyDescriptorCollection …
Run Code Online (Sandbox Code Playgroud)

c# generics collections datatable nullable

74
推荐指数
4
解决办法
7万
查看次数

使用XmlSerializer将空xml属性值反序列化为nullable int属性

我从第三方获得了一个xml,我需要将它反序列化为C#对象.此xml可能包含值为整数类型或空值的属性:attr ="11"或attr ="".我想将此属性值反序列化为类型为可空整数的属性.但XmlSerializer不支持反序列化为可空类型.在使用InvalidOperationException创建XmlSerializer期间,以下测试代码失败{"有一个错误反映了类型'TestConsoleApplication.SerializeMe'."}.

[XmlRoot("root")]
public class SerializeMe
{
    [XmlElement("element")]
    public Element Element { get; set; }
}

public class Element
{
    [XmlAttribute("attr")]
    public int? Value { get; set; }
}

class Program {
    static void Main(string[] args) {
        string xml = "<root><element attr=''>valE</element></root>";
        var deserializer = new XmlSerializer(typeof(SerializeMe));
        Stream xmlStream = new MemoryStream(Encoding.ASCII.GetBytes(xml));
        var result = (SerializeMe)deserializer.Deserialize(xmlStream);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我将'Value'属性的类型更改为int时,反序列化失败并出现InvalidOperationException:

XML文档中存在错误(1,16).

任何人都可以建议如何将具有空值的属性反序列化为可空类型(作为null),同时将非空属性值反序列化为整数?有没有任何技巧,所以我不必手动对每个字段进行反序列化(实际上有很多)?

来自ahsteele的评论后更新:

  1. Xsi:nil属性

    据我所知,此属性仅适用于XmlElementAttribute - 此属性指定元素没有内容,无论是子元素还是正文.但我需要找到XmlAttributeAttribute的解决方案.无论如何我不能改变xml,因为我无法控制它.

  2. bool*指定的属性

    仅当属性值为非空或缺少属性时,此属性才有效.当attr具有空值(attr ='')时,XmlSerializer构造函数失败(如预期的那样).

    public class Element
    {
        [XmlAttribute("attr")]
        public int Value { get; …
    Run Code Online (Sandbox Code Playgroud)

.net xml serialization nullable

73
推荐指数
3
解决办法
6万
查看次数

什么是"DateTime?" 在C#中意味着什么?

我正在阅读.Net书籍,在其中一个代码示例中,有一个带有此字段的类定义:

private DateTime? startdate
Run Code Online (Sandbox Code Playgroud)

什么DateTime?意思?

.net c# syntax datetime nullable

70
推荐指数
5
解决办法
6万
查看次数

Linq查询具有可空和

from i in Db.Items
select new VotedItem
{
    ItemId = i.ItemId,
    Points = (from v in Db.Votes
              where b.ItemId == v.ItemId
              select v.Points).Sum()
}
Run Code Online (Sandbox Code Playgroud)

我得到了这个查询,但是如果没有找到异常的投票则它会失败:

The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.
Run Code Online (Sandbox Code Playgroud)

我假设它因为sum返回一个int而不是一个可空的int,给一个int一个?作为输入只给出相同的错误,可能导致sum仅适用于int.

有什么好的解决方法吗?

c# linq nullable sum linq-to-sql

69
推荐指数
6
解决办法
6万
查看次数

为什么.ToString()在空字符串上会导致空错误,当.ToString()在具有空值的可空int上正常工作?

selectedItem 有两个领域:

  • int? _cost
  • string _serialNumber

在这个例子中,_cost_serialNumberselectedItem都是空.我正在阅读通过selectedItem他们的属性的字段,并填写文本框与他们的值,当...

TextBox1.Text = selectedItem.Cost.ToString(); //no error
TextBox2.Text = selectedItem.SerialNumber.ToString(); //error
Run Code Online (Sandbox Code Playgroud)

我明白这SerialNumber.ToString()是多余的(因为它已经是一个字符串),但我不明白为什么这会导致这个异常:

可以为空的对象必须具有值.

  • int? _cost 可以为空,并且没有值,但它没有给我例外.
  • string _serialNumber可以为空,并且没有值,但它确实给了我例外.

这个问题触及它,这家伙基本上是在问同样的事情,但是没有指定的答案,而且它也没有解释为什么可以为空int?例如,我可以.ToString()在可空的int上使用但不能在空字符串上使用吗?

c# string int null nullable

69
推荐指数
3
解决办法
7626
查看次数