-1

I have two Dictionaries. One with a list of columns from an Excel sheet and a list of defined columns. I wanna know if the defined columns exists in the sheet. If they exists then they will selected in a chosen dropdownlist.

In the row dropdownList.SelectedValue = selectedItem.First().Key; I get sometimes an errormessage Sequence contains no elements. I thought that I had coded safety. What do I forget?

... the command ...

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

... and then the calling method ...

    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() will give that error when selectedItem is empty. - Mixxiphoid
  • This happens when columnList.Where(cl => cl.Value == defColumnList[index]) yields an empty sequence which you then call First() on. - spender
  • SelectedItem is going to produce a squence of items. When you check to see if its not null the sequence isnt null and calling First will cause this error. Its best to change your check to something like this var selectedItem = columnList.Where(cl => cl.Value == defColumnList[index]); if (selectedItem != null && selectedItem.Any()) dropdownList.SelectedValue = selectedItem.First().Key; - Bad Dub
  • I know that when First() is empty you get an error. But I was looking for something like this: if (selectedItem.Any()). And it works... - user1531040
  • I do understand now: The title of my question was wrong... I have changed this. - user1531040

2 답변


2

The meaning of this error is that selectedItem is having no element while you are trying to access first element which is not possible.

Instead of checking nullability you should check is there any element inside the collection and then should execute First method on collection.

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


1

.Where() operator returns an enumeration, and not a single element. So, your selectedeItem!=null condition always returns true. Change your code to this:

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


  • This code will also always evaluate selectedItem != null as true. - Enigmativity
  • If selectedItem != null, we will select it. Seems right to me. - Amy B
  • @DavidB - This code changes the semantics that the OP is looking for. It is incorrect. - Enigmativity
  • @Enigmativity no, OP tests the result of Where for null, which is always false. That is OP's mistake - it must be changed. - Amy B
  • @DavidB - Yes, but the default value for KeyValuePair<int, string> is never null. KeyValuePair<int, string> is a value-type. - Enigmativity

Linked


Latest