Estoy trabajando con la siguiente instrucción linq:
var suppliers = SupplierView.Select()
.GroupBy(x => x.Name.Substring(0, 1).ToUpper(),
(alphanumeric, suppliers) => new
{
Alphanumeric = alphanumeric,
Suppliers = suppliers.OrderBy(x => x.Name).ToList()
})
.OrderBy(x => x.Alphanumeric);
Me gustaría devolver esto desde un método, sin embargo, para hacerlo creo que necesitaré usar un tipo concreto en lugar de un tipo anónimo.
Aquí está el tipo de concreto:
public class AlphanumericSuppliers
{
public string Alphanumeric { get; set; }
public IOrderedEnumerable<Supplier> Suppliers { get; set; }
}
Sin embargo, estoy teniendo dificultades para obtener la sintaxis correcta para convertir el tipo anónimo a mi tipo concreto.
Alguien aconsejo?
Respuestas
6 para la respuesta № 1Simplemente cree instancias del tipo concreto (no hay ningún requisito para usar un tipo anónimo en LINQ):
var suppliers = SupplierView.Select()
.GroupBy(x => x.Name.Substring(0, 1).ToUpper(),
(alphanumeric, suppliers) => new AlphanumericSuppliers // concrete
{
Alphanumeric = alphanumeric,
Suppliers = suppliers.OrderBy(x => x.Name).ToList() // *
})
.OrderBy(x => x.Alphanumeric);
* Como se menciona en los comentarios, ya sea cambio Suppliers
ser IEnumerable<Supplier>
o quitar el ToList
llama desde aqui