0

`null の結果をデータテーブルに書き込むのに問題があります。私の linq クエリは、クラスの新しいインスタンスに入力する値を返しています。私のデータテーブルは一般的に作成されており、一般的に作成されたデータ行が取り込まれています。

データテーブルが正常に作成され、クエリが実行されますが、VAR ステートメントをヒットすると、10 進数フィールドの 1 つが null であるため失敗します。データテーブルを作成できないため、クラスでこれを変更できません。

null値を受け入れるようにするには、次の1行を変更する必要があります。

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
             }

データテーブルとデータ行を作成する方法は次のとおりです

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クエリ」の後にコードのこの部分に到達するとすぐに、問題が発生します。

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);
        }

データベースから返される moneyvalue の値が null であるため、foreach に到達するとすぐに失敗します。

クラスピースを10進数に変更できませんか? それ以外の場合、DataTable は null 許容値を持つことができないため、CreateDatable メソッドは失敗します。

4

2 に答える 2

1

データベースに NULL 値を書き込むことが許可されている場合は、変数の型を null 可能にする必要があります。たとえば、

[Column]
public decimal? moneyvalue;

それ以外の

[Column]
public decimal moneyvalue;
于 2013-09-13T11:49:26.607 に答える
1

あなたの問題は

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
}

* アップデート *

@ user65439が述べたようdatatabletraded_product、DBクラス(t_sdi_traded_product)をnull許容列に変更してください

[Column]
public decimal? moneyvalue;

次に、返されたnullを処理し、traded_productクラスのnull不可の小数のためにそれらを0に変換する必要があります

于 2013-09-13T11:51:36.057 に答える