Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

199
Visualizações
IEnumerable Update At Index

I have to update a value in the IEnumerable collection. Because it is read only, what is the trick to update the values?

product.Variants is an IEnumerable and I want to update something like this

product.Variants[0].Barcode ="something"

current I get an error like this

enter image description here

then I pass product variable to a function and that does some processing on product.Variants

someFunction (product) // this function should have the new barcode updated above.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Error CS0021 documentation says:

An attempt was made to access a value through an indexer on a data type that does not support Indexers.

Simple as that. An IEnumerable just exposes the enumerator not the underlying data. If you want to access that data you need to "materialize" the IEnumerable into a collection of its data.

var list = product.Variants.ToList();
if(list.Count > 0)
{
    ProductVariant pv = list[0];
    ....
}

but this "materializes" the whole list and if you need only the first item probably you can just use the IEnumerable extension FirstOrDefault;

var pv = product.Variants.FirstOrDefault();
if(pv != null)
{
   .....
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda