I have three tables as:
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; }
}
I have DescriptionID
. What will be the linq to get all the ImageSection
s having that DescriptionID ?
var result = dbContext.Images.Include(i => i.ImageSections)
.Where(j => j.DescriptionID == 1)
.SelectMany(im => im.ImageSections).ToList();
I hope this will help
This should do the trick
var descriptions = new List<Description>();
var descriptionId = 1;
var result =
descriptions.Where(d => d.DescriptionID == descriptionId).SelectMany(
im => im.Image.SelectMany(ims=> ims.ImageSection));
It seems like you do not need to join three tables given the description of your problem. As you already have the DescriptionID
that you want to join on you can ignore the Description
table.
Try this:
var query1 =
from i in db.Images
where i.DescriptionID == descriptionID
join s in db.ImageSections on i.ImageID equals s.ImageID
select s;
However, since your table definitions already contain the child collections you could do a basic SelectMany
without worrying about joins per se.
Try this as an alternative:
var query2 =
from d in db.Descriptions
where d.DescriptionID == descriptionID
from i in d.Image
from s in i.ImageSection
select s;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.