28

Possible Duplicate:
The entity cannot be constructed in a LINQ to Entities query

   var tasks = from i in data.Incidents
                    join a in data.Accounts on i.CustomerID equals a.Acct_CID
                    select new Tasks()
                    {

                        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
                    };

        foreach (var task in tasks)
            data.Tasks.Add(task);
        data.SaveChanges();

 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; }
    }

gives me an error when I try to iterate, I would love some help with this

Community
  • 1
  • 1
Chazt3n
  • 1,641
  • 3
  • 17
  • 42

1 Answers1

84

In Linq-to-Entities you can only project to an anonymous type or a regular class. You can't project to an existing entity type. You can with linq-to-objects like so

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
              // ...
             }).AsEnumerable().Select(x => new Tasks {
               creator_id = x.creator_id,
               start_date = x.start_date,
               end_date = x.end_date              
             }).ToList();
Aducci
  • 26,101
  • 8
  • 63
  • 67
  • Error 9 Cannot initialize type 'TRX.CRM.Dashboard.Entities.DashBoard.Tasks' with a collection initializer because it does not implement 'System.Collections.IEnumerable' – Chazt3n Oct 16 '12 at 13:57
  • @Chazt3n - I added a `ToList` at the end. Give that a try – Aducci Oct 16 '12 at 14:02
  • The `ToList` doesn't fix this. You can't use implied field names if it's not an anonymous type. – jwg Jul 03 '14 at 11:54
  • 2
    Thanks a million Aducci; I have been looking around for a few days now trying to get rid of a few other errors I was encountering and I finally got rid of them all. – Artorias2718 Jun 16 '15 at 08:07
  • You saved my day :) – superachu Mar 16 '18 at 14:26