该类型出现在单个 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 查询中的两个结构不兼容的初始化中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Can I initialize a BCL immutable collection using braces?(我可以使用大括号初始化BCL不可变集合吗?)
The type initializer for #39;System.Data.Entity.Internal.AppConfig#39; threw an exception on a Sub Website(“System.Data.Entity.Internal.AppConfig的类型初始化程序在子网站上引发异常)
Possible to initialize multiple variables from a tuple?(可以从一个元组初始化多个变量吗?)
error loading database initializer with EF6(使用 EF6 加载数据库初始化程序时出错)
How to force fire OnModelCreating every DataContext initialized up(如何强制触发 OnModelCreating 每个初始化的 DataContext)
Does Stacklt;gt; constructor reverse the stack when being initialized from other one?(是否堆栈lt;gt;构造函数在从另一个初始化时反转堆栈?)