简体   繁体   English

如何使用SelectMany展平集合

[英]How to flatten collection using SelectMany

I have the following entities (I18N is a localized entity): 我有以下实体(I18N是本地化实体):

public class Post { 
  public Int32 Id { get; set; }
  public Boolean IsPublished { get; set; }
  public List<PostI18N> PostsI18N { get; set; }
  public List<Tag> Tags { get; set; }
  public Author { get; set; }
}

public class Tag { 
  public List<TagI18N> TagsI18N { get; set; }
}

public class Author { 
  public Int32 Id { get; set; }
  public String Name { get; set; }
}

public class PostI18N { 
  public Int32 Id { get; set; }
  public String Text { get; set; }
  public String Title { get; set; }
}

public class TagI18N { 
  public Int32 Id { get; set; }
  public String Name { get; set; }
}

I need to get all information of 4 posts so I tried to flatten the query: 我需要获取4个帖子的所有信息,所以我尝试拉平查询:

var posts = await _context
    .Posts
    .SelectMany(x => x.PostsI18N, (Post, PostI18N) => 
       new { Post, PostI18N, Post.Tags, Post.Author })
    .Where(x => x.PostI18N.Language == "en")
    .Select(x => new PostDTO {
       Id = x.Post.Id,
       Title = x.PostI18N.Title,
       Text = x.PostI18N.Text,
       AuthorName = x.Author.Name
       TagsNames = // Names taken from x.Tags.TagsI18N where TagsI18N
                   // language is "en" ... So, for each tag look the 
                   // one Tag.TagI18N which Tag.TagI18N.Language = "en"
                   // and get Tag.TagI18N.Name
    })
    .Take(4)
    .ToListAsync();

PROBLEM : The problem is that I also need the TagsI18N flatten so I can take their names for English language ... 问题 :问题是我还需要将TagsI18N展平,以便我可以使用它们的英文名称...

It this possible with SelectMany? SelectMany有可能吗? How should I do this? 我应该怎么做?

Try it in query syntax instead: 尝试使用查询语法:

var posts = await (
    from p in _context.Posts
    from pn in p.PostsI18N
    where pn.Language == "en"
    select new PostDTO {
        Id = p.Id,
        Title = pn.Title,
        Text = pn.Text,
        AuthorName = p.Author.Name,
        TagsNames = from t in p.Tags
                    from tn in t.TagsI18N
                    where tn.Language == "en"
                    select tn.Name
    }).Take(4).ToListAsync();

The SelectMany syntax should work as well, but it gets a bit "nested": SelectMany语法也应该可以正常工作,但是有点“嵌套”:

var posts = await _context
    .Posts
    .SelectMany(x => x.PostsI18N, (Post, PostI18N) => 
        new { Post, PostI18N, Post.Tags, Post.Author })
    .Where(x => x.PostI18N.Language == "en")
    .Select(x => new PostDTO {
        Id = x.Post.Id,
        Title = x.PostI18N.Title,
        Text = x.PostI18N.Text,
        AuthorName = x.Author.Name
        TagsNames =
            x.Tags.SelectMany(t => t.TagsI18N, (Tag, TagI18N) =>
                new { Tag, TagI18N })
            .Where(t => t.TagI18N.Language == "en")
            .Select(t => t.TagI18N.Name)
    })
    .Take(4)
    .ToListAsync();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM