[英]Joining three tables using linq
我有三个表:
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; }
}
我有DescriptionID
。 获取具有该DescriptionID的所有ImageSection
的linq是什么?
var result = dbContext.Images.Include(i => i.ImageSections)
.Where(j => j.DescriptionID == 1)
.SelectMany(im => im.ImageSections).ToList();
我希望这个能帮上忙
这应该可以解决问题
var descriptions = new List<Description>();
var descriptionId = 1;
var result =
descriptions.Where(d => d.DescriptionID == descriptionId).SelectMany(
im => im.Image.SelectMany(ims=> ims.ImageSection));
给出问题的说明,您似乎不需要连接三个表。 由于已经有了要加入的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
您可以执行基本的SelectMany
而不必担心联接本身。
尝试以下方法:
var query2 =
from d in db.Descriptions
where d.DescriptionID == descriptionID
from i in d.Image
from s in i.ImageSection
select s;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.