板块类
    [Table("tb_board")]
    public class BoardEntity
    {
        [Key]
        public int BoardID { set; get; }        [Required,MaxLength(255)]
        public string Title { set; get; }        [Required, MaxLength(500)]
        public string Keywords { set; get; }        [Required, MaxLength(500)]
        public string Desc { set; get; }        public int Sort { set; get; }        public bool IsDisplay { set; get; }        public bool IsDelete { set; get; }        [Required, Column(TypeName = "smalldatetime")]
        public DateTime AddDate { set; get; }        public virtual ICollection<BoardItemEntity> ItemBoardEntitys { set; get; }
    }
子版块类
[Table("tb_board_item")]
    public class BoardItemEntity
    {
        [Key]
        public int BoardItemID { set; get; }        public int BoardID { set; get; }        [MaxLength(255)]
        public string Title { set; get; }        [MaxLength(500),Column(TypeName="varchar")]
        public string Photo { set; get; }        [Required,MaxLength(500)]
        public string Keywords { set; get; }        [Required, MaxLength(500)]
        public string Desc { set; get; }        public int Sort { set; get; }        public bool IsDisplay { set; get; }        public bool IsDelete { set; get; }        [Required, Column(TypeName = "smalldatetime")]
        public DateTime AddDate { set; get; }
    }我现在的查询方式是:
        public IEnumerable<BoardEntity> GetBoardList()
        {
            IEnumerable<BoardEntity> list = null;
            var query = from b in db.BoardEntitys
                        where b.IsDelete == false && b.IsDisplay == true
                        orderby b.Sort, b.AddDate ascending
                        select new
                        {
                            b.BoardID,
                            b.Title,
                            b.ItemBoardEntitys
                        };
            list = query.ToList().ConvertAll<BoardEntity>(item => new BoardEntity { BoardID = item.BoardID, Title = item.Title, ItemBoardEntitys = item.ItemBoardEntitys });
            return list;
        }我这样是可以查询出来,但是子版块我只想查询出部分字段,子版块也要排序,上面方法如何改进,谢谢!

解决方案 »

  1.   

    以后别用 code first 啦,不切实际。用代码描述数据库,你得非多大的劲啊?
    程序员用代码把技术经理、架构师、DBA等人物的设计数据库工作给抢了。DB first 的话  from b in db.BoardEntitys.Include("ItemBoardEntitys") 就连子版块一起出来了。
      

  2.   

    也可以用内连接,或者可以用外连接(如果你希望添加外部行的话)。// Inner Join.
    var query = from board in db.BoradEntitys
                join boardItem in db.BoardItemEntitys
                on board.BoardID equals boardItem.BoardID
                where <Your Condition>
                orderby <Your Condition>
                select
                {
                     new { BoardItemID = boardItem.BoardItemID, <Your Other Conditions> }
                }