这是我在 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。 " “指定的演员阵容无效”。这是它引发错误的地方:
[全局::System.Diagnostics.DebuggerNonUserCodeAttribute()] 公共 System.DateTime deal_date { 得到 { 尝试 { return ((global::System.DateTime)(this[this.tableImbalanceDetailForRealTime.deal_dateColumn])); } 捕捉(全局::System.InvalidCastException e){ throw new global::System.Data.StrongTypingException("表 \'ImbalanceDetailForRealTime\' 中列 \'deal_date\' 的值为 DBNull。" + "", e);//这里抛出错误 } } 放 { this[this.tableImbalanceDetailForRealTime.deal_dateColumn] = value; } }
- 我试图将“deal_date = b.deal_date”行替换为“deal_date = (DateTime?)b.deal_date”,但出现 2 个编译错误:“dep_pos_imbalance_detail.TableDataTable.AddTableRow(string, System. DateTime)' 有一些无效的参数”和“参数 '2': 不能从 'System.DateTime?' 转换” 到'System.DateTime'"
- 我还尝试将“deal_date = b.deal_date”行替换为“deal_date = b.deal_date == null ? (DateTime)DBNull.Value : b.deal_date”但我收到编译错误:“无法转换类型'System .DBNull' 到 System.DateTime'"
- 然后我尝试用“deal_date = b.deal_date == null ?(DateTime?)DBNull.Value:b.deal_date”替换“deal_date = b.deal_date”行但我收到编译错误:“无法转换类型' System.DBNull' 到 System.DateTime?'"
- 我尝试了另一件事:将“deal_date = b.deal_date”替换为“deal_date = b.Isdeal_dateNull() ? default(DateTime?) : b.deal_date” 但同样,我有以下错误:“最好的重载方法匹配dep_pos_imbalance_detail.TableDataTable.AddTableRow(string, System.DateTime)' 有一些无效参数”和“参数 '2':不能从 'System.DateTime 转换?' 到 'System.DateTime'" 下图(抱歉,我还不允许在 stackoverflow 中插入图像,所以我放了链接)显示了我的数据集中的列 deal_date 的定义: https://lh5.googleusercontent。 com/-TEZZ9Hdnkl4/T1aRxF_i7II/AAAAAAAAAAg/BwzrVXIlOHE/s323/deal_date.jpg 我们可以看到我似乎没有可能设置“System.DateTime?” 但只有“System.DateTime”。而且我不想要除了 null 作为默认值之外的任何其他值(我们是否必须放置默认值“”以外的其他内容才能使其工作?)更新->我尝试使用 null 而不是设计师给出了这个错误:“字符串未被识别为有效的日期时间。从索引 0 开始有一个未知单词。”。
所以我不明白我怎么能设法检索空值(我没有把它放在代码中,但我对 double 类型有同样的问题)。我的印象是我的列设置为启用空值,但显然不是......此外,当我尝试修改 NullValue 属性以从“(抛出异常)”到“(空)”或“(空)”时,设计者给出这个错误:“输入的值对当前数据类型无效。” 感谢您的帮助。这是我的 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);
}