简体   繁体   English

Linq中的StrongTypingException

[英]StrongTypingException in Linq

Anybody can tell why the ToList() throws an exception? 任何人都可以告诉为什么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) 
{ ... }

The Exception message is: 异常消息是:
'duplicates.ToList()' threw an exception of type 'System.Data.StrongTypingException' System.Collections.Generic.List {System.Data.StrongTypingException} 'duplicates.ToList()'引发了类型'System.Data.StrongTypingException'的异常System.Collections.Generic.List {System.Data.StrongTypingException}

Thanks 谢谢

From MSDN: 来自MSDN:

StrongTypingException StrongTypingException

The exception that is thrown by a strongly typed DataSet when the user accesses a DBNull value. 用户访问DBNull值时由强类型DataSet引发的异常。

So the problem occurs because one of the properties you access in the query is null. 因此,出现此问题的原因是您在查询中访问的某个属性为null。 Check which properties of your DataTable are allowed to be null, and check it with a call to IsNull before you try to get the value. 检查DataTable哪些属性允许为空,并在尝试获取值之前通过调用IsNull进行检查。

Try to use that to eleminate Null value 尝试使用它来消除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};

juste to test without null value juste测试没有空值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM