【问题标题】:C# How can I prevent in this code that this error message occurs: Sequence contains no elements? [duplicate]C# 如何防止在此代码中出现此错误消息:序列不包含元素? [复制]
【发布时间】:2017-06-06 10:50:52
【问题描述】:

我有两个字典。一个包含 Excel 工作表中的列列表和已定义列的列表。我想知道工作表中是否存在定义的列。如果它们存在,那么它们将在选定的下拉列表中被选中。

在行 dropdownList.SelectedValue = selectedItem.First().Key; 我有时会收到错误消息序列不包含任何元素。我以为我已经编码了安全性。我忘记了什么?

...命令...

SetDataSource(import.ColumnList, import.DefColumnList, ddlSomeColumn, SomeEnum.Zipcode);

...然后调用方法...

    private void SetDataSource(Dictionary<int, string> columnList, Dictionary<int, string> defColumnList, DropDownList dropdownList, SomeEnum item)
    {
        int index = (int)item;
        dropdownList.BeginUpdate();
        dropdownList.ValueMember = "Key";
        dropdownList.DisplayMember = "Value";
        dropdownList.DataSource = columnList;
        if (defColumnList.ContainsKey(index) && defColumnList[index].Length > 0)
        {
            var selectedItem = columnList.Where(cl => cl.Value == defColumnList[index]);
            if (selectedItem != null)
                dropdownList.SelectedValue = selectedItem.First().Key;
        } 
        dropdownList.EndUpdate();
    }

【问题讨论】:

  • selectedItem.First() 将在 selectedItem 为空时给出该错误。
  • columnList.Where(cl =&gt; cl.Value == defColumnList[index]) 产生一个空序列时会发生这种情况,然后您调用First() on。
  • SelectedItem 将产生一系列项目。当您检查它是否不为空时,序列不为空并调用First 将导致此错误。最好将您的检查更改为类似 var selectedItem = columnList.Where(cl => cl.Value == defColumnList[index]); if (selectedItem != null && selectedItem.Any()) dropdownList.SelectedValue = selectedItem.First().Key;
  • 我知道当 First() 为空时,您会收到错误消息。但我一直在寻找这样的东西:if (selectedItem.Any())。它有效......
  • 我现在明白了:我的问题的标题是错误的......我已经改变了这个。

标签: c# linq dictionary


【解决方案1】:

此错误的含义是 selectedItem 在您尝试访问不可能的第一个元素时没有元素。

您应该检查集合中是否有任何元素,而不是检查可空性,然后应该在集合上执行First 方法。

var selectedItem = columnList.Where(cl => cl.Value == defColumnList[index]);
if (selectedItem.Any())
    dropdownList.SelectedValue = selectedItem.First().Key;

【讨论】:

    【解决方案2】:

    .Where() 运算符返回一个枚举,而不是单个元素。因此,您的 selectedeItem!=null 条件始终返回 true。将您的代码更改为:

    private void SetDataSource(Dictionary<int, string> columnList, Dictionary<int, string> defColumnList, DropDownList dropdownList, SomeEnum item)
        {
            int index = (int)item;
            dropdownList.BeginUpdate();
            dropdownList.ValueMember = "Key";
            dropdownList.DisplayMember = "Value";
            dropdownList.DataSource = columnList;
            if (defColumnList.ContainsKey(index) && defColumnList[index].Length > 0)
            {
                var selectedItem = columnList.FirstOrDefault(cl => cl.Value == defColumnList[index]);
                if (selectedItem != null)
                    dropdownList.SelectedValue = selectedItem.Key;
            } 
            dropdownList.EndUpdate();
        }
    

    【讨论】:

    • 此代码也将始终将selectedItem != null 评估为true
    • 如果 selectedItem != null,我们将选择它。对我来说似乎是正确的。
    • @DavidB - 此代码更改了 OP 正在寻找的语义。这是不正确的。
    • @Enigmativity no,OP 测试 Where 的结果是否为 null,始终为 false。那是 OP 的错误——它必须被改变。
    • @DavidB - 是的,但KeyValuePair&lt;int, string&gt; 的默认值绝不是nullKeyValuePair&lt;int, string&gt; 是一个值类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多