query in partial classes

May 1 2020 2:08 PM
Hello, good afternoon everyone, I have a problem making a list with linq with join, I have the following classes
  1. namespace CapaDatos  
  2. {  
  3.     using System;  
  4.     using System.Collections.Generic;  
  5.       
  6.     public partial class tblCategoria  
  7.     {  
  8.         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2214:DoNotCallOverridableMethodsInConstructors")]  
  9.         public tblCategoria()  
  10.         {  
  11.             this.tblProductos = new HashSet<tblProducto>();  
  12.             this.tblDetalleVentas = new HashSet<tblDetalleVenta>();  
  13.         }  
  14.       
  15.         public int Id { getset; }  
  16.         public string Descripcion { getset; }  
  17.         public byte[] Fotografia { getset; }  
  18.         public Nullable<int> Estado_Id { getset; }  
  19.       
  20.         public virtual tblEstado tblEstado { getset; }  
  21.         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2227:CollectionPropertiesShouldBeReadOnly")]  
  22.         public virtual ICollection<tblProducto> tblProductos { getset; }  
  23.         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2227:CollectionPropertiesShouldBeReadOnly")]  
  24.         public virtual ICollection<tblDetalleVenta> tblDetalleVentas { getset; }  
  25.     }  
  26. }  
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
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace CapaDatos  
  8. {  
  9.     public partial class tblCategoria  
  10.     {  
  11.         string NombreEstado;  
  12.         public static void GuardarCategoria(tblCategoria tblCategoria)  
  13.         {  
  14.             using (GourmetEntities db = new GourmetEntities())  
  15.             {  
  16.                 db.tblCategorias.Add(tblCategoria);  
  17.                 db.SaveChanges();  
  18.             }  
  19.         }  
  20.         public static List<tblCategoria> ListaCategorias()  
  21.         {  
  22.             using (GourmetEntities db = new GourmetEntities())  
  23.             {  
  24.                 var Lista = (from List in db.tblCategorias  
  25.                              join e in db.tblEstados on List.Estado_Id equals e.Id  
  26.                              select new tblCategoria  
  27.                              {  
  28.                                     Descripcion = List.Descripcion,  
  29.                                     NombreEstado = e.NombreEstado, // this field comes from the tblStates table  
  30.                              }).ToList();  
  31.   
  32.                 return Lista;  
  33.                 //return db.tblCategorias.ToList(); ok  
  34.             }  
  35.         }  
  36.     }  
  37. }  
 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
 

Answers (1)