简体   繁体   English

清单中的项目不存在时掉落如何检查

[英]Falling over when item does not exist in list how to check

I have this linq query but its falling over when no gender has been set against the user it says squencce 我有这个linq查询,但是当没有针对用户设置性别时,它就会崩溃

List<StandardLookUpList> _AnalsisCodes = GetAnayalsisCodesForExportCode();

var codesForThisItem = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode);
if (codesForThisItem.Count()  > 0 )
{
     if (codesForThisItem.First(x => x.code == Constants.Sport) != null)
      sport = codesForThisItem.First(x => x.code == Constants.Sport);

       if(codesForThisItem.First(x => x.code == Constants.Gender) !=null)
       gender = codesForThisItem.First(x => x.code == Constants.Gender);
}     

I thought that this line was enough to fix this?. 我认为这行足以解决此问题?

if (codesForThisItem.First(x => x.code == Constants.Sport)  

But its actually the code for this item is failing I cant use count to strap that as it may have the other codes held against it what is my best way of trapping if its not in the list replace with null string instead. 但是,实际上该项目的代码失败了,我无法使用count来捆绑它,因为如果它不在列表中,请替换为空字符串,这可能是其他最好的捕获方法。

you can use .FirstOrDefault() instead and then check whether the result is null before proceeding. 您可以改用.FirstOrDefault() ,然后在继续操作之前检查结果是否为null。 The problem with what you've written is that .First() always expects there to be a matching result: 您编写的问题是.First()始终期望会有匹配的结果:

List<StandardLookUpList> _AnalsisCodes = GetAnayalsisCodesForExportCode();
var codesForThisItem = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode);

if (codesForThisItem.Any())
{
     var sportResult = codesForThisItem.FirstOrDefault(x => x.code == Constants.Sport);
     if (sportResult != null) sport = sportResult;

     var genderResult = codesForThisItem.FirstOrDefault(x => x.code == Constants.Gender); 
     if (genderResult != null) gender = genderResult;
}

In fact, if it's always OK for sport and gender themslves to potentially be null (I don't know what they're set to before this code runs or what rules you have about them), you could just do: 实际上,如果sportgender自我总是可能为空(我不知道在代码运行前将它们设置为什么,或者对它们有什么规则),则可以这样做:

List<StandardLookUpList> _AnalsisCodes = GetAnayalsisCodesForExportCode();
var codesForThisItem = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode);

if (codesForThisItem.Any())
{
     sport = codesForThisItem.FirstOrDefault(x => x.code == Constants.Sport);
     gender = codesForThisItem.FirstOrDefault(x => x.code == Constants.Gender); 
}

Use FirstOrDefault instead of First . 使用FirstOrDefault代替First First will throw an exception (Sequence contains no matching element) when your predicate does not match any elements. 当您的谓词不匹配任何元素时,First将抛出一个异常(Sequence不包含匹配的元素)。

List<StandardLookUpList > _AnalsisCodes = GetAnayalsisCodesForExportCode();

var codesForThisItem = _AnalsisCodes.Where(w => w.ItemCode == item.ItemCode);
if (codesForThisItem.Any())
{
    if (codesForThisItem.FirstOrDefault(x => x.code == Constants.Sport) != null)
    {
        sport = codesForThisItem.First(x => x.code == Constants.Sport);
    }

    if (codesForThisItem.FirstOrDefault(x => x.code == Constants.Gender) != null)
    {
        gender = codesForThisItem.First(x => x.code == Constants.Gender);
    }
}

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

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