使用Linq左连接多个IList <>

用户名

我基本上有一个帖子库,应该返回所有属于它的图库项目。如果没有属于该帖子的画廊,它仍应返回按帖子ID区分的帖子

public List<PostLocalizedOutput> GetAllPostsWithCategories(string culture, bool? isPublished)
    {
        var query =
                from p in Context.Posts
                join pl in Context.PostsLocalized on p.Id equals pl.PostId
                from c in p.Categories
                join cl in Context.CategoriesLocalized on c.Id equals cl.CategoryId
                from g in p.Galleries.DefaultIfEmpty()
                join gi in Context.GalleryItems on g.Id equals gi.GalleryId 


                where
                  pl.Culture == culture &&
                  cl.Culture == culture
                select new PostLocalizedOutput
                {
                    PostId = pl.PostId,
                    CategoryId = cl.CategoryId,
                    Title = pl.Title,
                    FormattedCategoryName = cl.FormattedCategoryName,
                    PostContent = pl.PostContent,
                    PostType = pl.Post.PostType,
                    IOrder = pl.Post.IOrder,
                    Tags = pl.Tags,
                    PublishDate = pl.Post.PublishDate,
                    ViewCount = pl.Post.ViewCount,
                    ShowInHomePageSlider = pl.Post.ShowInHomePageSlider,
                    AllowComments = pl.Post.AllowComments,
                    Image = pl.Post.Image,
                    IsArchived = pl.Post.IsArchived,
                    IsDraft = pl.Post.IsDraft,
                    IsPublished = pl.Post.IsPublished,
                    GalleryItems = new GalleryItemOutput
                    {
                        FileName = gi.FileName,
                        GalleryId = gi.GalleryId,
                        Id = gi.Id,
                        Notes = gi.Notes,
                        Title = gi.Title
                    } (around here i feel like i should foreach something or what?)
                };

        return query.OrderBy(x => x.IOrder).ThenBy(x => x.PublishDate).DistinctBy(x => x.PostId).ToList();
    }

这是我的postlocalized输出

public class PostLocalizedOutput : IOutputDto
{
    public int PostId { get; set; }

    public int CategoryId { get; set; }

    public bool IsPublished { get; set; }

    ...  
    public List<GalleryItemOutput> GalleryItems { get; set; }

}

GalleryItemOutput应该是列表,因为我想要发布的所有画廊。但是,当我将其定义为存储库中的列表时,我无法设置帖子的GalleryItem的每个字段。这段代码现在返回了四行,因为我有该帖子的四个图库项目,每个项目都有相同的postId。我不要那个。即使帖子中没有任何图库项目,DefaultIfEmpty也不起作用,我仍然应该能够在没有图库项目的情况下获得该帖子。

有什么办法吗?

感谢您的所有建议。

约翰·伦斯比

这应该可以帮助您入门。不知道它是否可以编译,请解释为伪代码。看起来您的顶级范围变量是PostsLocalized而不是Posts。此代码假定/使用了可能在您的EDM类中设置的导航属性。我在这里过度使用了let关键字只是为了让您更清楚。在select new子句中,您可以将p1.Post ....更改为p。我离开了他们进行尽可能少的编辑。

var query =
        from pl in Context.PostsLocalized
        where pl.Culture == culture
        let p = p1.Post
        let categories = p.Categories
        let localizedCategories = categories.SelectMany(cat => cat.CategoriesLocalized).Where(cl => cl.Culture == culture)
        let galleries = p.Galleries
        let galleryItems = galleries.SelectMany(gal => gal.GalleryItems)
        let cl = localizedCategories.FirstOrDefault() // only one or zero of these i assume?

        select new PostLocalizedOutput
        {
            PostId = p1.PostId,
            CategoryId = cl.CategoryId, 
            Title = pl.Title,
            FormattedCategoryName = cl.FormattedCategoryName,
            PostContent = pl.PostContent,
            PostType = pl.Post.PostType,
            IOrder = pl.Post.IOrder,
            Tags = pl.Tags,
            PublishDate = pl.Post.PublishDate,
            ViewCount = pl.Post.ViewCount,
            ShowInHomePageSlider = pl.Post.ShowInHomePageSlider,
            AllowComments = pl.Post.AllowComments,
            Image = pl.Post.Image,
            IsArchived = pl.Post.IsArchived,
            IsDraft = pl.Post.IsDraft,
            IsPublished = pl.Post.IsPublished,

            GalleryItems = galleryItems.Select(gi => new GalleryItemOutput
            {
                FileName = gi.FileName,
                GalleryId = gi.GalleryId,
                Id = gi.Id,
                Notes = gi.Notes,
                Title = gi.Title
                })
// might need a .ToList() here on those GalleryItems

 (around here i feel like i should foreach something or what?)
            };

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用光滑的多个左连接?

来自分类Dev

LInq在on子句中具有多个条件的左连接

来自分类Dev

LINQ 查询左连接多个列,空检查失败

来自分类Dev

Linq 使用左连接如何使用 order by 和 group by?

来自分类Dev

使用LINQ将2个表与主表左连接

来自分类Dev

使用 LINQ 连接多个表以返回列表

来自分类Dev

Linq Count左外连接

来自分类Dev

MS Access 多个左连接

来自分类Dev

LINQ右连接和左连接

来自分类Dev

LINQ查询中左外部连接子句上的多个条件

来自分类Dev

Linq 2左外连接,第二列有多个列

来自分类Dev

多个外部Linq连接

来自分类Dev

使用左连接顺序

来自分类Dev

使用左连接计数

来自分类Dev

使用LINQ通过IList的内容过滤DbSet

来自分类Dev

使用lambda语法的多个带有匿名类型的linq连接

来自分类Dev

使用lambda语法的多个带有匿名类型的linq连接

来自分类Dev

在实体框架中使用 Linq 使用只有外键的表的左外连接

来自分类Dev

LINQ与group by左连接并正确计数

来自分类Dev

LINQ-3个ObservableCollections的左连接

来自分类Dev

LINQ与group by左连接并正确计数

来自分类Dev

Linq to Sql 与左连接不同,Lambda

来自分类Dev

它在 linq 中使用左连接跨越两个表与两个字段

来自分类Dev

带导轨的多个左连接4

来自分类Dev

带导轨的多个左连接4

来自分类Dev

具有相同列的多个左连接

来自分类Dev

将左连接与 where 用于多个表

来自分类Dev

Hive 上的多个左外连接

来自分类Dev

使用If条件选择多个字段,并在mysql中进行左连接-抛出错误