The sequence contains no elements

5

This is the error it gives.

  

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

Below is my expression that generated the 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();
}

The error appears above the refeicoes line. This array has a value, which in this case is: "Breakfast for 2" . Does anyone have a clue to solve this cucumber?

Personally, I discovered that the above loop is wrong. It can not be on top of meals, but on top of Hotel search, as meals will always come with a record. It is mounted with a click on top of the checkbox. I retraced the loop, swapping meals for research. I just did it and now it gives the error like that. When i == 0, it adds to the list. but when i is greater than zero, there it goes, saying "Index out of bounds". How do I solve this? Below the new code. Remembering that in this example, meals have 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();
}
    
asked by anonymous 08.04.2014 / 16:45

2 answers

10

This error occurs in the Linq% method call, when the collection in which you call it is empty.

You have two options:

  • Review the snippet that says First() - this selection does not return any items. You may need to review all the logic until you get to this point. It is highly recommended to break your code in more steps, so you can better isolate at what point your queries do not return what you expect;

  • Use the method Select(a=> a.ProductId).First() instead of FirstOrDefault() . That way, instead of throwing an exception, you'll have a null reference. Then you can treat it as convenient for you.

08.04.2014 / 16:58
0

It was resolved like this:

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

I would like to vote for Renan's answer, how do I do it? I received an email where the modereras here gave me some warnings. I received the warnings as a positive yes. I here in the company, I can not read the rules of the forum and I confess I'm kind of lost. It is a forum very different from the others. From the warnings I received, only one can not agree, that was to be clearer in the questions. This "clarity" is purely relative. We ask in a way that we understand. But other than that, I'll pay more attention to everything I post, but with caveats. I have not yet become familiar with the forum, but this next holiday, I will read the rules, faqs and etc, not to make mistakes, or at least minimize them. I'm writing this here, because I could not reply to the email they sent me and I do not like receiving email and not being able to respond. I think that's wrong. But I hope to collaborate to improve this forum and what depends on me, will be done, for sure.

    
09.04.2014 / 15:14