该类型出现在单个 LINQ to Entities 查询中的两个结构不兼容的初始化中

The type appear in two structurally incompatible initializations within a single LINQ to Entities query(该类型出现在单个 LINQ to Entities 查询中的两个结构不兼容的初始化中)

本文介绍了该类型出现在单个 LINQ to Entities 查询中的两个结构不兼容的初始化中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从多个表中获取记录,最后两个表与之相关,因此我在它们之间进行连接,但第一个表与它们没有关系,但我需要从中获取数据.这是我的视图模型

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();

推荐答案

这两个片段

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

 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,
 }

第二个

 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.

这篇关于该类型出现在单个 LINQ to Entities 查询中的两个结构不兼容的初始化中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:该类型出现在单个 LINQ to Entities 查询中的两个结构不兼容的初始化中

基础教程推荐