Linq中的StrongTypingException

时间:2012-01-10 09:51:25

标签: c# linq linq-to-sql linq-to-objects

任何人都可以告诉为什么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.StrongTypingException'的异常System.Collections.Generic.List {System.Data.StrongTypingException}

由于

2 个答案:

答案 0 :(得分:3)

来自MSDN:

  

StrongTypingException

     

用户访问DBNull值时强类型DataSet引发的异常。

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

答案 1 :(得分:0)

尝试使用它来消除Null值

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

只是为了测试没有空值