无法在LINQ to Entities查询中构造实体或复杂类型“WebProject.Data.Value”。“,

时间:2015-07-30 14:03:48

标签: c# linq entity-framework

我在Linq to Entities查询中使用复杂类型时遇到问题。我想做的是生成嵌套的json对象,但我在使用Value作为我的值类中的属性时遇到了问题

 public class ItemManagementViewModel
    {
        public string FormType { get; set; }
        public long FormControllerID { get; set; }
        public string FormControllerName { get; set; }
        public IEnumerable<Value> values { get; set; }
    }

        public class Value
        {
            public long RecordId { get; set; }
            public string Name { get; set; }
            public long? ParentId { get; set; }
            public Value Parent { get; set; }
        }
            var innerResult = (from innerValue1 in db.Values
                               join innerValue2 in db.Values on innerValue1.ParentId equals innerValue2.RecordId
                               join innerValue3 in db.Values on innerValue1.RecordId equals innerValue3.ParentId
                               where
                                   innerValue1.ParentId
                                   == (from va in db.Values where va.Name == formType select va.RecordId)
                                          .FirstOrDefault()
                               orderby innerValue1.RecordId
                               select
                                   new Value
                                       {
                                           RecordId = innerValue3.RecordId,
                                           Name = innerValue2.Name,
                                           ParentId = innerValue3.RecordId
                                       }).ToList();  

1 个答案:

答案 0 :(得分:0)

我使用匿名类型我的linq查询

        var innerResult = (from innerValue1 in db.Values
                           join innerValue2 in db.Values on innerValue1.ParentId equals innerValue2.RecordId
                           join innerValue3 in db.Values on innerValue1.RecordId equals innerValue3.ParentId
                           where
                               innerValue1.ParentId
                               == (from va in db.Values where va.Name == formType select va.RecordId)
                                      .FirstOrDefault()
                           orderby innerValue1.RecordId
                           select new 
                                   {
                                       RecordId = innerValue3.RecordId,
                                       Name = innerValue2.Name,
                                       ParentId = innerValue3.RecordId
                                   }).ToList();