0

我有三个表:

 public class Description
    {
        public int DescriptionID { get; set; }

        // Attributes 

        public virtual List<Image> Image { get; set; }     

    }

public class Image
    {
        public int ImageID { get; set; }

        // Attributes       

        [Required]
        public int DescriptionID { get; set; }

        [ForeignKey("DescriptionID")]
        public virtual Description Description { get; set; }

        public virtual List<ImageSection> ImageSection { get; set; }
    }



public class ImageSection
    {
        public int ImageSectionID { get; set; }

        // Attributes

        public int? ImageID { get; set; }

        [ForeignKey("ImageID")]
        public virtual Image Image { get; set; }

     }

我有DescriptionIDImageSection获取具有该 DescriptionID 的所有 s 的 linq 将是什么?

4

3 回答 3

1
var result = dbContext.Images.Include(i => i.ImageSections)
                 .Where(j => j.DescriptionID == 1)
                 .SelectMany(im => im.ImageSections).ToList(); 

我希望这个能帮上忙

于 2012-07-14T09:17:52.783 回答
0

这应该可以解决问题

var descriptions = new List<Description>();

            var descriptionId = 1;

            var result =
                descriptions.Where(d => d.DescriptionID == descriptionId).SelectMany(
                    im => im.Image.SelectMany(ims=> ims.ImageSection));
于 2012-07-14T09:14:46.047 回答
0

鉴于您的问题描述,您似乎不需要加入三个表。由于您已经拥有DescriptionID要加入的表格,因此可以忽略该Description表格。

试试这个:

var query1 =
    from i in db.Images
    where i.DescriptionID == descriptionID
    join s in db.ImageSections on i.ImageID equals s.ImageID
    select s;

但是,由于您的表定义已经包含子集合,您可以做一个基本的操作SelectMany而不用担心连接本身。

试试这个作为替代方案:

var query2 =
    from d in db.Descriptions
    where d.DescriptionID == descriptionID
    from i in d.Image
    from s in i.ImageSection
    select s;
于 2012-07-15T05:04:25.783 回答