1

重複
の可能性: エンティティまたは複合型 ' ' は、LINQ to Entities クエリでは構築できません

先日この質問をしましたが、実際には 100% 有効な回答が見つかりませんでした。2 つのテーブル (インシデントとアカウント) を比較し、顧客 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

匿名オブジェクトと定義された型のオブジェクトをインスタンス化する方法に混乱していると思います。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 に答える
1

Tasksクラスを次のように仮定します。

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 に答える