StrongTypingException in Linq

本文关键字:Linq in StrongTypingException | 更新日期: 2023-09-27 18:19:56

有人能说出ToList()抛出异常的原因吗?

var duplicates =  
    from typeMappings in _liveTable.Where(r =>
        (r.ProviderId == providerId) && (r.ExchangeId == exchangeId))
    join dataDictionary in _liveDataSet.DataDictionary.Where(r => 
        (r.DataDictionaryTypeId == dataDictionaryTypeId)) 
    on typeMappings.DataDictionaryId equals dataDictionary.DataDictionaryId
    select typeMappings.ConfigId;
if (duplicates.ToList().Count > 0) 
{ ... }

异常消息为:
"duplicates.ToList()"引发了类型为"System.Data.StrongTypeingException"的异常System.Collections.Generic.List{System.Data.SStrongTypeingException}

感谢

StrongTypingException in Linq

来自MSDN:

StrongTypeingException

当用户访问DBNull值时,强类型数据集引发的异常。

因此,问题的出现是因为您在查询中访问的某个属性为null。检查DataTable的哪些属性允许为null,并在尝试获取值之前通过调用IsNull进行检查。

尝试使用它来命名空值

var duplicates =  
from typeMappings in _liveTable.Where(r =>
    (r.ProviderId == providerId) && (r.ExchangeId == exchangeId))
join dataDictionary in _liveDataSet.DataDictionary.Where(r => 
    (r.DataDictionaryTypeId == dataDictionaryTypeId)) 
on typeMappings.DataDictionaryId equals dataDictionary.DataDictionaryId
select new
       { ConfigId = typeMappings.ConfigId = null ? "anyValueyouwhant" : typeMappings.ConfigId};

只需在没有空值的情况下进行测试