Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#将空字段写入数据表_C#_Linq_Generics_Datatable_Nullable - Fatal编程技术网

c#将空字段写入数据表

c#将空字段写入数据表,c#,linq,generics,datatable,nullable,C#,Linq,Generics,Datatable,Nullable,`我在将空结果写入数据表时遇到问题。 我的linq查询返回的值将填充一个类的新实例。 我的datatable是以常规方式创建的,并由常规创建的datarow填充 发生的情况是成功创建了我的datatable,查询运行,但当我点击VAR语句时,它失败了,因为其中一个十进制字段为null。我无法在类中更改此项,因为这样我就无法创建datatable 我需要更改这一行,使其接受空值: moneyvalue = result.moneyvalue, 这是我的表定义: [Table(Name = "t_

`我在将空结果写入数据表时遇到问题。 我的linq查询返回的值将填充一个类的新实例。 我的datatable是以常规方式创建的,并由常规创建的datarow填充

发生的情况是成功创建了我的datatable,查询运行,但当我点击VAR语句时,它失败了,因为其中一个十进制字段为null。我无法在类中更改此项,因为这样我就无法创建datatable

我需要更改这一行,使其接受空值:

moneyvalue = result.moneyvalue,
这是我的表定义:

[Table(Name = "t_sdi_traded_product")]
public class t_sdi_traded_product
{
    [Column]
    public string deal_position_id;
    [Column]
    public decimal moneyvalue;
    [Column]
    public string cost_centre;
}
这是我的课

 public class traded_product
{
    public string Deal { get; set; }
    public decimal moneyvalue { get; set; }
    public string InvolvedPartyId { get; set; }
}
这是我的问题

 var query =
            from result in t_sdi_traded_product_hsbc.AsQueryable()
            where result.sdi_control_id == current_control_id
            select new traded_product() 
            { 
                Deal = result.deal_position_id, 
                moneyvalue = result.moneyvalue,
                InvolvedPartyId = result.involved_party_id
             }
下面是我如何创建datatable和datarow的

public static DataTable CreateDataTable(Type animaltype)
    {
        DataTable return_Datatable = new DataTable();
        foreach (PropertyInfo info in animaltype.GetProperties())
        {
            return_Datatable.Columns.Add(new DataColumn(info.Name, info.PropertyType));
        }
        return return_Datatable;
    }

    public static DataRow makeRow(object input, DataTable table)
    {
        Type inputtype = input.GetType();
        DataRow row = table.NewRow();
        foreach (PropertyInfo info in inputtype.GetProperties())
        {
            row[info.Name] = info.GetValue(input, null);
        }
        return row;
    }
现在,在“var query”之后,只要它碰到代码的这一部分,我就会发现问题:

foreach (var results in query)
        {
            foreach (PropertyInfo result in results.GetType().GetProperties())
            {
                string name = result.Name;

                foreach (PropertyInfo info in used.GetType().GetProperties())
                {
                    if (result.Name == info.Name)
                    {
                        try
                        {
                            info.SetValue(used, result.GetValue(results, null), null);
                        }
                        catch (NoNullAllowedException e)
                        {
                        }
                        finally
                        {
                            info.SetValue(used, DBNull.Value, null);
                        }
                        //Console.WriteLine("Result {0} matches class {1} and the value is {2}", result.Name, info.Name, result.GetValue(results,null));
                    }
                }
            }
            tp_table.Rows.Add(used, tp_table);
        }
它一碰到foreach就失败,因为从数据库返回的moneyvalue值为null


我不能把课文改成十进制?否则CreateDatable方法会失败,因为它说DataTable不能有可为空的值。

例如,如果允许将空值写入数据库,则应使变量类型可为空

[Column]
public decimal? moneyvalue;
而不是

[Column]
public decimal moneyvalue;

我想你的问题是

select new traded_product() 
{ 
    Deal = result.deal_position_id, 
    moneyvalue = result.moneyvalue, <-- here you need some handling for DBNULL.Value
    InvolvedPartyId = result.involved_party_id
}

select new traded_product() 
{ 
    Deal = result.deal_position_id, 
    moneyvalue = result.moneyvalue == DBNull.Value ? 0m : result.moneyvalue,
    InvolvedPartyId = result.involved_party_id
}

然后,您只需处理返回的空值,并将它们转换为0,表示您的
traded\u产品
类中不可为空的十进制数

我也在尝试。我将[列]设置为十进制?我有下面的
outval(result.funding\u rate.ToString())?(object)result.funding_rate:(object)DBNull.Value,
其中outval是一个bool方法,用于检查它是否为十进制,但仍然会收到一个错误,说明无法将类型object转换为decimal@SecretSquirrel@user65439,这可能是因为您正在强制转换到一个对象。outval=result.funding_rate==DBNull.Value;
[Column]
public decimal? moneyvalue;