models层
public class BlogEntity
    {
        public int ID { get; set; } 
       public string COLName { get; set; }
        
    }    public interface ICOLRepository {
          IList<COL> ListAll();
     }   public class COLRepository : ICOLRepository {
        private DataClasses1DataContext _dataContext;        public COLRepository() {
            _dataContext = new DataClasses1DataContext();
        }        #region IMovieRepository Members        public IList<BlogEntity> ListAll()
        {
            var BlogEntity= from m in _dataContext.COL
                         select new { m.COLName,m.ID};            return BlogEntity.ToList();                       return BlogEntity.ToList<BlogEntity>();
        }        #endregion
    }返回应该怎么写BlogEntity.ToList()和BlogEntity.ToList<BlogEntity>();都出错

解决方案 »

  1.   

    var BlogEntity= from m in _dataContext.COL
      select new { m.COLName,m.ID};这儿查出来的跟你的实体并不一致吧,这儿只有两列
      

  2.   

    什么错误吗?select new { m.COLName,m.ID};这样返回的是dynamic object动态对象,在View层中应该用dynamic做为Model对象,这个只有.net 4.0才支持。
      

  3.   

    错误 1 无法将类型“System.Collections.Generic.List<AnonymousType#1>”隐式转换为“System.Collections.Generic.IList<Mvc6.Models.BlogEntity>”。存在一个显式转换(是否缺少强制转换?) D:\MVC\Mvc6\Mvc6\Models\MainModels.cs
      

  4.   

    那就是我说的问题,你用select new { m.COLName,m.ID};获取的是匿名对象,而不是Mvc6.Models.BlogEntity
      

  5.   

     public class BlogEntity
        {
            public string COLName { get; set; }
                   
        }    public interface ICOLRepository {
            IList<BlogEntity> ListAll();
         }   public class COLRepository : ICOLRepository {
            private DataClasses1DataContext _dataContext;        public COLRepository() {
                _dataContext = new DataClasses1DataContext();
            }        #region IMovieRepository Members        public IList<BlogEntity> ListAll()
            {
                var BlogEntity = from m in _dataContext.COL
                                 select new { m.COLName};            return BlogEntity.ToList();            //return BlogEntity.ToList<BlogEntity>();
            }        #endregion
        }
    返回值应该怎么写呢
      

  6.   

    没必要写那么多,一句就行了:
    return _dataContext.COL.ToList();
      

  7.   

    根据你的需求,及返回的结果 ,建个 多表实体层,参考 http://topic.csdn.net/u/20090314/22/e2d895aa-92f7-45a7-97b5-f76c5ff3de23.html