Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

c# - string tripping - Additional information: Sequence contains no elements

I have a weird bug im running into.

I am getting: Additional information: Sequence contains no elements and its being tripped on string sheetNum = "";

I usually get this error when a ling function can't find anything so I am completely lost on why this is getting tripped off here.

Also, not sure if this is a result of my updating my Visual Studio IDE recently.

here is the code block that its in. its a button for winforms:

private void createSheetButton_Click(object sender, EventArgs e)
        {
            string sheetNum = ""; // <--- getting error here
            bool sheetNumExistsBool = false;
            if (colSheetsList.Count() > 0)
            {
                sheetNum = colSheetsList.First().get_Parameter(BuiltInParameter.SHEET_NUMBER).AsString(); 
                sheetNumExistsBool = colSheetsList.Any(x => x.get_Parameter(BuiltInParameter.SHEET_NUMBER).AsString() == sheetNumTextBox.Text);
            }
            //... more code here 
       }

enter image description here

question from:https://stackoverflow.com/questions/65849616/string-tripping-additional-information-sequence-contains-no-elements

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue is that if the colSheetsList catch the 0 line, the rest of the code will continue to handle 0 line which is not suitable for it and will cause such error. You should add a non-zero operation.

You should use FirstOrDefault() as Cflux said instead. When it catch 0 line, it will break the rest of the code and return null to prevent such error.

Solution

Use FirstOrDefault()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...