首页 新闻 搜索 专区 学院

一对多的关系,如何LINQ TO SQL 获取

0
悬赏园豆:5 [已关闭问题] 关闭于 2012-09-07 16:35
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.ComponentModel.DataAnnotations;
 6 using System.Data.Entity;
 7 
 8 namespace Mydream.Entity
 9 {
10     [Table("tb_board")]
11     public class BoardEntity
12     {
13         [Key]
14         public int BoardID { set; get; }
15 
16         [Required,MaxLength(255)]
17         public string Title { set; get; }
18 
19         [Required, MaxLength(500)]
20         public string Keywords { set; get; }
21 
22         [Required, MaxLength(500)]
23         public string Desc { set; get; }
24 
25         public int Sort { set; get; }
26 
27         public bool IsDisplay { set; get; }
28 
29         public bool IsDelete { set; get; }
30 
31         [Required, Column(TypeName = "smalldatetime")]
32         public DateTime AddDate { set; get; }
33 
34         public virtual ICollection<BoardItemEntity> ItemBoardEntitys { set; get; }
35     }
36 }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;

namespace Mydream.Entity
{
    [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; }
    }
}

我获取 IEnumerable<BoardEntity>的时候,如何查询出BoardItemEntitys

xu_happy_you的主页 xu_happy_you | 菜鸟二级 | 园豆:222
提问于:2012-09-07 15:55
< >
分享
所有回答(1)
0

 using (var context = new TKInfDBContext())
            {
                return (from m in context.Dic_POPValueDef.Include("Dic_POPTypeDef").Include("Dic_POPTypeDef.Dic_POPClassDef")
                        where m.VID == vid
                             select m).FirstOrDefault();
            }

这是使用DBContext来实现的一对多的查询。不过是Linq to entity 的方式。Linq To Sql的写法,还真没试过。

jerry-Tom | 园豆:4077 (老鸟四级) | 2012-09-07 16:41
支持(0) 反对(0) xu_happy_you | 园豆:222 (菜鸟二级) | 2012-09-07 16:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册