实体或复杂类型''不能在一个LINQ被构造为实体的查询 [英] The entity or complex type ' ' cannot be constructed in a LINQ to Entities query

查看:105
本文介绍了实体或复杂类型''不能在一个LINQ被构造为实体的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/5325797/the-entity-cannot-be-constructed-in-a-linq-to-entities-query\">The实体不能在一个LINQ被构造成实体查询


 在data.Incidents从我VAR任务=
                    在data.Accounts加入上i.CustomerID等于a.Acct_CID
                    选择新的任务()
                    {                        creator_id = a.ID,
                        START_DATE = i.DateOpened,
                        END_DATE = i.DateCLosed,
                        product_ code =一,产品code,
                        INSTALL_TYPE = i.InstallType,
                        OS = i.OSType,
                        细节= i.Description,
                        解= i.Solution,
                        CREATOR_NAME = i.TechID,
                        类别= I.标题,
                        文字=票的++ i.Name,
                        STATUS_ID = 7
                    };        的foreach(在任务VAR任务)
            data.Tasks.Add(任务);
        data.SaveChanges(); 公共类事件
    {
        [键]
        公众诠释IncidentID {搞定;组; }
        公共字符串客户ID {搞定;组; }
        公共字符串产品code {搞定;组; }
        公共字符串TechID {搞定;组; }
        公众的DateTime DateOpened {搞定;组; }
        公众的DateTime DateCLosed {搞定;组; }
        公共字符串名称{搞定;组; }
        公共字符串描述{搞定;组; }
        公共字符串解决方案{搞定;组; }
        公共字符串名称{;组; }
        公共字符串OSTYPE {搞定;组; }
        公共字符串将InstallType {搞定;组; }
        公共字符串AddOnSoftware {搞定;组; }
        公共字符串萤幕{搞定;组; }
    }

给我一个错误,当我尝试重复,我喜欢一些帮助,这


解决方案

在LINQ到实体你只能项目,匿名类型或普通班。你不能预测到现有的实体类型。你可以使用LINQ到对象,像这样

  VAR任务=(从我在data.Incidents
            在data.Accounts加入上i.CustomerID等于a.Acct_CID
             新选择
             {
               creator_id = a.ID,
               START_DATE = i.DateOpened,
               END_DATE = i.DateCLosed
              // ...
             })AsEnumerable()选择(X =方式&gt;新的任务{
               creator_id = x.creator_id,
               START_DATE = x.start_date,
               END_DATE = x.end_date
             })了ToList()。

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

解决方案

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();

这篇关于实体或复杂类型''不能在一个LINQ被构造为实体的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆