The type appear in two structurally incompatible initializations within a single LINQ to Entities query

c# entity-framework-6 linq

Question

I am trying to get the record from multiple tables the last two tables have relation with so i make a join between them but first table has no relation with them but i need to get the data from it. This is my ViewModel

 public class AssignTodoVM
{
    public int TOdoPresetTeamID { get; set; }
    public int UserId { get; set; }
    public int TaskId { get; set; }       
    public string TaskName { get; set; }
    public DateTime CreatedDate { get; set; }
    public string userName { get; set; }
    public int? TeamId { get; set; }

}

I wrote the following query and get the error.

 var Tasks = (from c in _context.ToDoPresets
                     select new AssignTodoVM
                     {
                         TaskId = c.ToDoPresetID,
                         TaskName = c.Title,
                         TOdoPresetTeamID = c.ToDoPresetID,
                     }).Union(
                     from q in _context.AppTeamMembers
                     join s in _context.AppUsers
                     on q.AppUserID equals s.UserId
                     where q.IsManager == false
                     select new AssignTodoVM
                     {

                         CreatedDate = System.DateTime.Now,

                         UserId = s.UserId,
                         userName = s.UserName,
                         TeamId = q.AppTeamID
                     }).ToList();

Error Screen

1
0
4/28/2018 9:33:30 AM

Accepted Answer

These two fragments

 new AssignTodoVM
 {
     TaskId = c.ToDoPresetID,
     TaskName = c.Title,
     TOdoPresetTeamID = c.ToDoPresetID,
 }

and

 new AssignTodoVM
 {

     CreatedDate = System.DateTime.Now,

     UserId = s.UserId,
     userName = s.UserName,
     TeamId = q.AppTeamID
 }

create objects of the same type but with different initializations. You need to make one of them similar to the other. Here's an attempt (which is probably incorrect, because I don't know the specifics of your classes); make the first one

 new AssignTodoVM
 {
     CreatedDate = System.DateTime.Now,
     UserId = -1,
     userName = "preset",
     TeamId = -1,
     TaskId = c.ToDoPresetID,
     TaskName = c.Title,
     TOdoPresetTeamID = c.ToDoPresetID,
 }

and the second one

 new AssignTodoVM
 {

     CreatedDate = System.DateTime.Now,    
     UserId = s.UserId,
     userName = s.UserName,
     TeamId = q.AppTeamID,
     TaskId = -1, // this is very likely wrong!
     TaskName = "",
     TOdoPresetTeamID = -1,
 }

As I said, this is probably not the correct answer, but I don't have enough information about your classes to make it better.

1
4/28/2018 8:02:07 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow