The sequence contains no elements

Asked

Viewed 7,533 times

6

That’s the mistake that comes.

An Exception of type 'System.Invalidoperationexception' occurred in System.Core.dll but was not handled in user code

Below my expression that generated error:

if (refeicoes != null)
{
    for (int i = 0; i < refeicoes.Length; i++) {
        lista.Add(pesquisaHotel.Where(x => x.SubOfferGroups.Select(a => a.AnswerOffersList[0].IncludesItems).Contains(refeicoes[i])).Select(a=> a.ProductId).First());
        lista.Add(pesquisaHotel.Where(x => x.SubOfferGroups.Select(a => a.AnswerOffersList[0].IncludesFoodPlans).Contains(refeicoes[i])).Select(a => a.ProductId).First());
    }

    pesquisaHotel = pesquisaHotel.Where(x => lista.Contains(x.ProductId)).ToArray();
}

Error appears on top of line refeicoes. This array has a value, which in this case is: "Breakfast for 2". Somebody’s got a tip on how to fix this cucumber?

Guys, I found out the above loop is wrong. It can not be on top of meals, but on top of researchHotel, because meals will always come with a record. It is mounted with a click on top of the checkbox. I redid the loop, changing refeicoes for searchHotel. I only did this and now gives the error like this. When i == 0, it adds in the list. But when i is greater than zero, then it sucks, saying "Index out of bounds". How do I fix this? Below the new code. Remembering that in this example, refeicoes has only one record.

if (refeicoes != null)
{

    for (int i = 0; i < pesquisaHotel.Length; i++)
    {
        lista.Add(pesquisaHotel.Where(x => x.SubOfferGroups.Select(a => a.AnswerOffersList[0].IncludesFoodPlans).Contains(refeicoes[i])).Select(a => a.ProductId).FirstOrDefault());
        lista.Add(pesquisaHotel.Where(x => x.SubOfferGroups.Select(a => a.AnswerOffersList[0].IncludesItems).Contains(refeicoes[i])).Select(a => a.ProductId).FirstOrDefault());
    }

    pesquisaHotel = pesquisaHotel.Where(x => lista.Contains(x.ProductId)).ToArray();
}
  • This variable "refeicoes" is what? An array?

  • Yes, an array of strings

  • Then, I search the value contained in this array in two fields and add the product in the list by its ID

  • If it is an array then the check has to be done like this: if(refeicoes[Indice] != null){...}

  • It is obvious that the new code will give error. If the variable refeicoes is an array that has 1 element, so the only possible index to be used is the 0, any index greater than 0 will give exception.

2 answers

11

This error happens in the method call First() of Linq, when the collection in which you call him is empty.

You have two options:

  • Review the passage that says Select(a=> a.ProductId).First() - this selection does not return any item. You may need to review all the logic until it comes to this point. It is highly recommended to break your code in more steps, so you can better isolate at which point your queries do not return what you expect;

  • Use the method FirstOrDefault() instead of First(). This way, instead of making an exception, you will have a null reference. Then you can treat as it is most convenient.

  • 1

    Jewelry Renan, I think that’s right. What happens now. See that I did two add, why? Sometimes Includesitems sometimes comes empty and sometimes no. And the same for Includesfoodsplans, because both have the same information (Portuguese thing) and in this example I happened to the first, In cludesItems be empty. I’ll switch to Firstordefault().

0

It was resolved thus:

string[] produtoId ;

if (refeicoes != null)
            {

                for (int i = 0; i < refeicoes.Length; i++)
                {
                    produtoId = pesquisaHotel.Where(x => x.SubOfferGroups.Select(a => a.AnswerOffersList[0].IncludesFoodPlans).Contains(refeicoes[i])).Select(a => a.ProductId).ToArray();

                    for (int x = 0; x < produtoId.Length; x++)
                    {
                        lista.Add(produtoId[x]);
                    }

                    produtoId = pesquisaHotel.Where(x => x.SubOfferGroups.Select(a => a.AnswerOffersList[0].IncludesItems).Contains(refeicoes[i])).Select(a => a.ProductId).ToArray();

                    for (int x = 0; x < produtoId.Length; x++)
                    {
                        lista.Add(produtoId[x]);
                    }

                }


                pesquisaHotel = pesquisaHotel.Where(x => lista.Contains(x.ProductId)).ToArray();
            }

Would you like to vote on Renan’s reply, as I do? I received an email where the moderas here made me some warnings. I received the warnings as something positive yes. I here at the company, I can not read the rules of the forum and confess I’m half lost. It is a very different forum from the others. Of the warnings I received, only one I cannot agree, which was to be clearer in the questions. This "clarity" is purely relative. We ask in a way we understand. But other than that, I’ll pay more attention to everything I post, but with caveats. I have not yet familiarized myself with the forum, but in this coming holiday, I will read the rules, faqs and so on, so as not to make mistakes, or at least minimize them. I am writing this here because I could not reply in the email they sent me and I do not like to receive email and not be able to answer. I think this is wrong. But I hope to collaborate to improve this forum and what depends on me, will be done for sure.

  • Below the triangle with a number (initially zero) is the button that aceita the answer.

  • 4
  • I don’t understand the question, Miguel.

  • 3

    @pnet This was not a question, but a link to the help center.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.