Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda