c# - ViewModel. Load LINQ query to view -


this question has answer here:

im using ef 6 , need linq query fetch data viewmodel. every event has status (one one) there not fk in tables

i want display that:

eventid   eventstatusid   name      eventdate   eventdesc     99        1               allowed   2000-1-1    music festival 

viewmodel class:

public class eventvm : event {     public eventstatus estatus { get; set; } } 

event class:

[serializable] public class event {     public int eventid { get; set; }     public int eventstatusid { get; set; }     public string eventnumber { get; set; }     public datetime eventdate { get; set; }     public string eventdesc { get; set; } } 

event status class:

[serializable] public class eventstatus {     public int eventstatusid { get; set; }     public string name{ get; set; }     public string desc{ get; set; } } 

event table:

dbo.event      column name     type       allow nulls pk  eventid         int        no     eventstatusid   int        no     eventnumber     nvarchar   no     eventdate       date       no     eventdesc       nvarchar   no 

eventstatus table:

dbo.eventstatus      column name     type       allow nulls pk  eventstatusid   int        no     desc            nvarchar   no     name            nvarchar   no 

i stucked in moment :

            var viewmodel =            (from ev in db.events             join evs in db.eventstatus on ev.eventstatusid equals evs.eventstatusid             ev.eventstatusid == id              select new eventvm             {                 eventid = ev.eventid             }).first(); 

with error:

system.notsupportedexception: 'the entity or complex type 'modelevent.entityframework.concrete.eventvm' cannot constructed in linq entities query.'

i cant change view model, have use one

you need create model or use anonymous type gather data db (don't forget use tolist() gather data), , map it's properties properties of eventvm. getting error, because ef unable create instance of eventvm.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -