通过LinQ迭代实体结果

时间:2012-10-17 14:22:36

标签: c# linq

  

可能重复:
  The entity or complex type ‘ ’ cannot be constructed in a LINQ to Entities query

我前几天问了这个问题,实际上没有找到任何有效的答案%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; }
}

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

5 个答案:

答案 0 :(得分:2)

您的问题是,您正在尝试构建Tasks个实例,但是您传入的项目与集合初始化程序相同,而不是像 object 初始值设定项或构造函数参数。

您需要更改

.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的方式。

答案 1 :(得分: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
    });

答案 2 :(得分: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
    };

答案 3 :(得分: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
});

答案 4 :(得分: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运算符中实例化一个已定义类的对象时,必须将值分配给该类的属性。否则,编译器将初始化的结果视为集合。这就是您的查询未编译的原因。