The type appear in two structurally incompatible initializations within a single LINQ to Entities query(该类型出现在单个 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 查询中的两个结构不兼容的初始化中
				
        
 
            
        - MoreLinq maxBy vs LINQ max + where 2022-01-01
 - 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
 - Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
 - C#MongoDB使用Builders查找派生对象 2022-09-04
 - WebMatrix WebSecurity PasswordSalt 2022-01-01
 - 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
 - 如何用自己压缩一个 IEnumerable 2022-01-01
 - 输入按键事件处理程序 2022-01-01
 - 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
 - C# 中多线程网络服务器的模式 2022-01-01
 
						
						
						
						
						
				
				
				
				