0
public List<TicketViewModel> GetTickets()
{
    DBEntities db = new DBEntities();

    List<TicketViewModel> tickets =
        (from ticket in db.Tickets
            join user in db.AspNetUsers on ticket.user_id equals user.Id
            join cat in db.TicketCategories on ticket.sub_category_id equals cat.id
            where ticket.is_active == true
            select new TicketViewModel
            {
                username = user.UserName,
                id = ticket.id,
                subject = ticket.subject,
                message = ticket.message,
                user_id = ticket.user_id,
                priority = (int)ticket.priority,
                date_created = ticket.date_created,
                date_modified = ticket.date_modified,
                assigned_to = ticket.assigned_to,
                is_active = ticket.is_active,
                sub_category_id = (int)ticket.sub_category_id,
                attatchments = ticket.attatchments,
                ticket_type = cat.name,
            }).ToList();

    return tickets;
} 

I am receiving the above error whilst trying to obtain a list of tickets. The TicketViewModel is an 'extended' ViewModel of the TicketModel which contains the additional username and category parameters not stored in the initial TicketModel class.

I have read Only parameterless constructors and initializers are supported in LINQ to Entities but this doesn't seem to cover the use of ViewModels in Linq

Not sure what I'm doing wrong here, can anyone help?

Edit: Added TicketViewModel class below.

public class TicketViewModel
{

    private DBEntities db = new DBEntities();

    [DisplayName("Ticket ID")]
    public int id { get; set; }

    [DisplayName("User ID")]
    public string user_id { get; set; }

    [DisplayName("Username")]
    public string username { get; set; }

    [DisplayName("Subject")]
    [Required(ErrorMessage = "required")]
    public string subject { get; set; }

    [DisplayName("Message")]
    [Required(ErrorMessage = "required")]
    public string message { get; set; }

    [DisplayName("Active")]
    public bool is_active { get; set; }

    [DisplayName("Date Created")]
    public DateTime date_created { get; set; }

    [DisplayName("Date Modified")]
    public DateTime date_modified { get; set; }

    [DisplayName("Category")]
    public int sub_category_id { get; set; }

    [DisplayName("Assigned To")]
    public string assigned_to { get; set; }

    [DisplayName("Attatchments")]
    public string attatchments { get; set; }

    [DisplayName("Priority")]
    public int priority { get; set; }

    public List<Attatchment> _attatchments = new List<Attatchment>();

    public string ticket_type = "";

    public int comments = 0;

    public List<SelectListItem> PrioritySelection = new List<SelectListItem>(){
        new SelectListItem(){Value = "1", Text = "Low"},
        new SelectListItem(){Value = "2", Text = "Medium"},
        new SelectListItem(){Value = "3", Text = "High"}
    };
}
Community
  • 1
  • 1
  • Can you represent the database table structure too.. – Moumit Feb 25 '16 at 18:01
  • Should there be a comma after cat.name? – Duston Feb 25 '16 at 18:23
  • have you tried the second solution from the SO link you provided? – Pavan Teja Feb 25 '16 at 18:33
  • Post the definition for `TicketViewModel` (and any inherited classes). – D Stanley Feb 25 '16 at 19:29
  • Is there any additional code in the constructor or property setters of `TicketViewModel/TicketModel`? – Gert Arnold Feb 25 '16 at 20:20
  • Updated post to include TicketViewModel. I have tried using the below code successfully with a foreach loop to set username and category names but its longwinded and ideally I would like to do it as per above code. `List tickets = db.Tickets.Where(t => t.is_active == false).ToList(); List _tickets = new List();` – Charlie Stelling Feb 26 '16 at 13:58

0 Answers0