以下是我在linq查询中使用的数据表的配置: 我有2个数据集文件(所有表的所有列都指定了DataType,其AllowDbNull属性设置为True): * deposit_position_imbalance.xsd: 包含2个数据: - 不平衡 - ImbalanceDetailForRealTime * dep_pos_imbalance_detail.xsd: 包含1个数据表: - 表
在下面的代码中,问题在于2行“deal_date = b.deal_date”。 实际上,当我从具有空值的数据库b.deal_date中检索时,它在deposit_position_imbalance.Designer.cs中说: “用户代码未处理StrongTypingException” “表'ImbalanceDetailForRealTime'中列'deal_date'的值是DBNull。” “指定演员表无效”。 这是它抛出错误的地方:
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] public System.DateTime deal_date { get { try { return ((global::System.DateTime)(this[this.tableImbalanceDetailForRealTime.deal_dateColumn])); } catch (global::System.InvalidCastException e) { throw new global::System.Data.StrongTypingException("The value for column \'deal_date\' in table \'ImbalanceDetailForRealTime\' is DBNull." + "", e);//ERROR THROWN HERE } } set { this[this.tableImbalanceDetailForRealTime.deal_dateColumn] = value; } }
所以我不明白我是如何设法检索空值的(我没有把它放在代码中,但我对double类型有同样的问题)。我的印象是我的列设置为启用空值但显然不是...... 此外,当我尝试修改NullValue属性以从“(Throw Exception)”变为“(Empty)”或“(Null)”时,设计器会给出以下错误:“输入的值对当前数据类型无效“。 谢谢您的帮助。 这是我的LINQ查询:
deposit_position_imbalance.ImbalanceDataTable dtImbalanceForRealTime;
deposit_position_imbalance.ImbalanceDetailForRealTimeDataTable dtImbalanceDetailForRealTime;
dtImbalanceForRealTime = (deposit_position_imbalance.ImbalanceDataTable)(((deposit_position_imbalance)(dataManager.GetConfig(grid1).ParentDataSource)).Imbalance);
dtImbalanceDetailForRealTime = this.detailForRealTime;
// we separate security_id null and not null
// Security id is not null
deposit_position_imbalance.ImbalanceDataTable iWithSecurityIdNotNull = new deposit_position_imbalance.ImbalanceDataTable();
deposit_position_imbalance.ImbalanceRow[] dr1 = (deposit_position_imbalance.ImbalanceRow[])dtImbalanceForRealTime.Select("security_id is not null");
if (dr1.Count<deposit_position_imbalance.ImbalanceRow>() > 0)
{
DataTable looselyTypedDT1 = dr1.CopyToDataTable<deposit_position_imbalance.ImbalanceRow>();
iWithSecurityIdNotNull.Merge(looselyTypedDT1, true);
}
// Security id is null
deposit_position_imbalance.ImbalanceDataTable iWithSecurityIdNull = new deposit_position_imbalance.ImbalanceDataTable();
deposit_position_imbalance.ImbalanceRow[] dr2 = (deposit_position_imbalance.ImbalanceRow[])dtImbalanceForRealTime.Select("security_id is null");
if (dr2.Count<deposit_position_imbalance.ImbalanceRow>() > 0)
{
DataTable looselyTypedDT2 = dr2.CopyToDataTable<deposit_position_imbalance.ImbalanceRow>();
iWithSecurityIdNull.Merge(looselyTypedDT2, true);
}
var queryWithSecurityIdFound =
from a in iWithSecurityIdNotNull
join b in dtImbalanceDetailForRealTime
on new
{
a.situation_date,
a.security_id,
a.deposit_location_id,
a.account_keeper_id
}
equals new
{
b.situation_date,
b.security_id,
b.deposit_location_id,
b.account_keeper_id
}
where a.situation_date == situation_date
&& a.security_id == security_id
&& a.deposit_location_id == deposit_location_id
&& a.account_keeper_id == account_keeper_id
select new
{
name = a.bo_source_name,
deal_date = b.deal_date
};
var queryWithSecurityIdNotFound =
from a in iWithSecurityIdNull
join b in dtImbalanceDetailForRealTime
on new
{
a.situation_date,
a.security_code,
a.deposit_location_id,
a.account_keeper_id
}
equals new
{
b.situation_date,
b.security_code,
b.deposit_location_id,
b.account_keeper_id
}
where a.situation_date == situation_date
&& a.security_id == security_id
&& a.deposit_location_id == deposit_location_id
&& a.account_keeper_id == account_keeper_id
select new
{
name = a.bo_source_name,
deal_date = b.deal_date
};
var query_final = queryWithSecurityIdFound.Union(queryWithSecurityIdNotFound);
//We fill the 'dep_pos_imbalance_detail Table'
grid1.Clear();
foreach (var item in query_final)
{
((dep_pos_imbalance_detail.TableDataTable)grid1.DataSet.Tables["Table"]).AddTableRow(item.name, item.deal_date);
}
答案 0 :(得分:1)
如果这是strongly typed DataSet
,它会自动生成您应该使用的Isdeal_dateNull
等可为空的列的属性。
if (!row.Isdeal_dateNull)
{
//do something
}
答案 1 :(得分:0)
我找到了解决问题的方法。在我的LINQ查询中,我将“deal_date = b.deal_date”替换为 “deal_date = b.Field('deal_date')”。然后可以进行转换。然后,我可以不使用Designer自动生成的方法“AddTableRow”,因为它不期望正确的类型。但我写这个,有点长但有效:
dep_pos_imbalance_detail.TableDataTable dt = ((dep_pos_imbalance_detail.TableDataTable)grid1.DataSet.Tables["Table"]);
dep_pos_imbalance_detail.TableRow dr = dt.NewTableRow();
foreach (var item in query_final)
{
dr = dt.NewTableRow();
dr.name = item.name;
if (item.deal_date.HasValue)
dr.deal_date = item.deal_date.Value;
else
dr.Setdeal_dateNull();
dt.AddTableRow(dr);
}