1

可能重复:
无法在 LINQ to Entities 查询中构造实体或复杂类型“”

前几天我问了这个问题,实际上没有找到任何有效的答案 %100 我正在尝试比较两个表(事件和帐户)并将具有匹配客户 ID 的事件分配给任务。

   var tasks = (from i in data.Incidents
                     join a in data.Accounts on i.CustomerID equals a.Acct_CID
                     select new
                     {
                         creator_id = a.ID,
                         start_date = i.DateOpened,
                         end_date = i.DateCLosed,
                         product_code = i.ProductCode,
                         install_type = i.InstallType,
                         os = i.OSType,
                         details = i.Description,
                         solution = i.Solution,
                         creator_name = i.TechID,
                         category = i.Title,
                         text = "Ticket for" + " " + i.Name,
                         status_id = 7
                     }).ToArray().Select(x => new Tasks
                     {
                         creator_id = x.creator_id,
                         start_date = x.start_date,
                         end_date = x.end_date,
                         product_code = x.product_code,
                         os = x.os,
                         details = x.details,
                         solution = x.solution,
                         creator_name = x.creator_name,
                         category = x.category,
                         text = x.text,
                         status_id = x.status_id
                     });

   foreach (var item in tasks)
   {
     data.Tasks.Add(item);
   }

这是任务类

 public class Tasks
      {
    [Key]
    public int id { get; set; }
    public string text { get; set; }
   // [CheckDateAtribute]
   [Display(Name="Start Date/Time")]
    [DataType(DataType.DateTime)]
    public DateTime start_date { get; set; }
    [DataType(DataType.DateTime)]
    [Display(Name = "End Date/Time")]
    public DateTime end_date { get; set; }
    [Display(Name="Details")]
    [Required]
    public string details { get; set; }
    public int owner_id { get; set; }
    public int creator_id { get; set; }
    public int status_id { get; set; }
    public string reply { get; set; }
    public string creator_name { get; set; }
    public string category { get; set; }
    public string solution { get; set; }
    public string os { get; set; }
    public string install_type { get; set; }
    public string product_code { get; set; }
}

事件类

   public class Incidents
{
    [Key]
    public int IncidentID { get; set; }
    public string CustomerID { get; set; }
    public string ProductCode { get; set; }
    public string TechID { get; set; }
    public DateTime DateOpened { get; set; }
    public DateTime DateCLosed { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string Solution { get; set; }
    public string Name { get; set; }
    public string OSType{ get; set; }
    public string InstallType { get; set; }
    public string AddOnSoftware { get; set; }
    public string ScreenShare { get; set; }
}

另一个编辑:现在得到一个超时异常

4

5 回答 5

2

您的问题是您正在尝试构造一个Tasks实例,但是您将项目作为集合初始化程序传递,而不是作为对象初始化程序或构造函数参数。

你需要改变

.Select(x => new Tasks{
     x.creator_id,
     x.start_date,
     ... });

进入

.Select(x => new Tasks{
     owner_id = x.creator_id,
     start_date = x.start_date,
     ... });

或者

.Select(x => new Tasks(
     x.creator_id,
     x.start_date,
     ... ));

取决于您通常如何构建Tasks.

于 2012-10-17T14:43:53.497 回答
1

假设任务类如下:

class Tasks
{
    public int CreatorID { get; set; } 
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int ProductCode { get; set; }
    public string InstallType { get; set; }
    public string OSType { get; set; }
    public string Details { get; set; }
    public string Solution { get; set; }
    public string CreatorName { get; set; }
    public string Category { get; set; }
    public string Text { get; set; }           
    public int StatusID { get; set; }  
}

用法:

var tasks = (from i in data.Incidents
    join a in data.Accounts on i.CustomerID equals a.Acct_CID
    select new Tasks()
    {                            
        CreatorID = a.ID,
        StartDate = i.DateOpened,
        EndDate = i.DateCLosed,
        ProductCode = i.ProductCode,
        InstallType = i.InstallType,
        OSType = i.OSType,
        Details = i.Description,
        Solution = i.Solution,
        CreatorName = i.TechID,
        Category i.Title,
        Text = "Ticket for" + " " + i.Name,
        StatusID = 7
    });
于 2012-10-17T14:29:15.167 回答
1
var tasks = 
    from i in data.Incidents
    join a in data.Accounts on i.CustomerID equals a.Acct_CID
    select new Task
    {                            
        creator_id = a.ID,
        start_date = i.DateOpened,
        end_date = i.DateCLosed,
        product_code = i.ProductCode,
        install_type = i.InstallType,
        os = i.OSType,
        details = i.Description,
        solution = i.Solution,
        creator_name = i.TechID,
        category = i.Title,
        text = "Ticket for" + " " + i.Name,
        status_id = 7
    };
于 2012-10-17T14:35:21.027 回答
1

试试这个:

var tasks =( from i in data.Incidents
join a in data.Accounts on i.CustomerID equals a.Acct_CID
select new 
{                            
    creator_id = a.ID,
    start_date = i.DateOpened,
    end_date = i.DateCLosed,
    product_code = i.ProductCode,
    install_type = i.InstallType,
    os = i.OSType,
    details = i.Description,
    solution = i.Solution,
    creator_name = i.TechID,
    category = i.Title,
    text = "Ticket for" + " " + i.Name,
    status_id = 7
}).ToArray().Select(x => new Tasks{
    creator_id = x.creator_id,
    start_date = x.start_date,
    end_date = x.end_date,
    product_code = x.product_code,
    os = x.os,
    details = x.details,
    solution = x.solution,
    creator_name = x.creator_name,
    category = x.category,
    text = x.text,
    status_id = x.status_id
});
于 2012-10-17T14:36:59.067 回答
1

我认为,您对我们实例化匿名对象和已定义类型的对象的方式感到困惑。如果我们从 Select 运算符中删除任务,则查询编译

var tasks = (from i in incidentsList
                       join a in accountsList on i.CustomerID equals a.Acct_CID
                       select new
                       {
                           creator_id = a.ID,
                           start_date = i.DateOpened,
                           end_date = i.DateCLosed,
                           product_code = i.ProductCode,
                           install_type = i.InstallType,
                           os = i.OSType,
                           details = i.Description,
                           solution = i.Solution,
                           creator_name = i.TechID,
                           category = i.Title,
                           text = "Ticket for" + " " + i.Name,
                           status_id = 7
                       }).AsEnumerable().Select(x => new
                     {
                         x.creator_id,
                         x.start_date,
                         x.end_date,
                         x.product_code,
                         x.os,
                         x.details,
                         x.solution,
                         x.creator_name,
                         x.category,
                         x.text,
                         x.status_id
                     });

但是,当我们在 Select 运算符中实例化已定义类的对象时,必须将值分配给类的属性。否则,编译器将初始化的结果视为一个集合。这就是您的查询未编译的原因。

于 2012-10-17T15:15:28.870 回答