Hello, good afternoon everyone, I have a problem making a list with linq with join, I have the following classes
- namespace CapaDatos
- {
- using System;
- using System.Collections.Generic;
-
- public partial class tblCategoria
- {
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
- public tblCategoria()
- {
- this.tblProductos = new HashSet<tblProducto>();
- this.tblDetalleVentas = new HashSet<tblDetalleVenta>();
- }
-
- public int Id { get; set; }
- public string Descripcion { get; set; }
- public byte[] Fotografia { get; set; }
- public Nullable<int> Estado_Id { get; set; }
-
- public virtual tblEstado tblEstado { get; set; }
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
- public virtual ICollection<tblProducto> tblProductos { get; set; }
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
- public virtual ICollection<tblDetalleVenta> tblDetalleVentas { get; set; }
- }
- }
As you will see, it is a partial class
therefore to create my methods create another class with the same name, but with a different file name, since I cannot create it with the same name
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace CapaDatos
- {
- public partial class tblCategoria
- {
- string NombreEstado;
- public static void GuardarCategoria(tblCategoria tblCategoria)
- {
- using (GourmetEntities db = new GourmetEntities())
- {
- db.tblCategorias.Add(tblCategoria);
- db.SaveChanges();
- }
- }
- public static List<tblCategoria> ListaCategorias()
- {
- using (GourmetEntities db = new GourmetEntities())
- {
- var Lista = (from List in db.tblCategorias
- join e in db.tblEstados on List.Estado_Id equals e.Id
- select new tblCategoria
- {
- Descripcion = List.Descripcion,
- NombreEstado = e.NombreEstado,
- }).ToList();
-
- return Lista;
-
- }
- }
- }
- }
The "StateName" field is not in the table or in the model
When I run the program and try to load into a datagridview it shows me the following
System.NotSupportedException: 'The entity or complex type 'GourmetModel.tblCategoria' cannot be constructed in a LINQ to Entities query.'
Esta excepción se generó originalmente en esta pila de llamadas:
[Código externo]
CapaDatos.tblCategoria.ListaCategorias() en OperacionCategorias.cs
CapaPresentacion.Frm_VisorImagenes.CargarDataGridCategorias() en Frm_VisorImagenes.cs
CapaPresentacion.Frm_VisorImagenes.Frm_VisorImagenes_Load(object, System.EventArgs) en Frm_VisorImagenes.cs
[Código externo]
I have both classes in the same layer and both are partial, please help me solve this problem, thank you all very much.
Ro