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?
– Matheus Bessa
Yes, an array of strings
– pnet
Then, I search the value contained in this array in two fields and add the product in the list by its ID
– pnet
If it is an array then the check has to be done like this: if(refeicoes[Indice] != null){...}
– Matheus Bessa
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 the0
, any index greater than 0 will give exception.– Miguel Angelo