C# 如何按不同的列表进行分组?
我正试图按照数据库中每个不同的标记对C# 如何按不同的列表进行分组?,c#,linq,C#,Linq,我正试图按照数据库中每个不同的标记对Posts序列进行分组 public class Post { public string Title { get; set; } public IEnumerable<string> Tags { get; set; } public static IEnumerable<Post> SeedPosts() { yield return new Post { Title = "Foo"
Post
s序列进行分组
public class Post
{
public string Title { get; set; }
public IEnumerable<string> Tags { get; set; }
public static IEnumerable<Post> SeedPosts()
{
yield return new Post { Title = "Foo", Tags = new[] { "Code" } };
yield return new Post { Title = "Foo1", Tags = new[] { "Code", "Productivity" } };
yield return new Post { Title = "Foo2", Tags = new[] { "Miscellaneous" } };
}
}
我完全被难住了,但我会尝试向你们展示我迄今为止所做的努力
我需要键
的类型为字符串
,但当我这样做时
posts.GroupBy(post => post.Tags);
键的类型为IEnumerable
。我知道我是按IEnumerable
进行分组的,因此键是IEnumerable
,但我通常还是被卡住了 试试这个:
posts
.SelectMany(p => p.Tags.Select(t => new {Tag = t, Post = p}))
.GroupBy(_ => _.Tag)
.ToDictionary(_ => _.Key, _ => _.Select(p => p.Post.Title).ToArray());
试试这个:
posts
.SelectMany(p => p.Tags.Select(t => new {Tag = t, Post = p}))
.GroupBy(_ => _.Tag)
.ToDictionary(_ => _.Key, _ => _.Select(p => p.Post.Title).ToArray());
在新列表或相同列表上展平列表
var posts = new List<Post>();
posts.Add(new Post { Title = "Foo", Tags = new[] { "Code" } } );
posts.Add(new Post { Title = "Foo1", Tags = new[] { "Code", "Productivity" } });
posts.Add(new Post { Title = "Foo2", Tags = new[] { "Miscellaneous" } });
var flattendPosts = new List<Post>();
foreach (var post in posts)
{
var tags = post.Tags.Select(tag => tag);
for (int i = 0; i < tags.Count(); i++)
{
flattendPosts.Add(new Post { Title = post.Title, Tag = post.Tags[i] });
}
}
flattendPosts.GroupBy(post => post.Tags);
var posts=newlist();
添加(newpost{Title=“Foo”,Tags=new[]{“Code”});
添加(新的Post{Title=“Foo1”,Tags=new[]{“Code”,“Productivity”});
添加(新的Post{Title=“Foo2”,Tags=new[]{“杂项”});
var flattedposts=新列表();
foreach(var post in post)
{
var tags=post.tags.Select(tag=>tag);
对于(int i=0;ipost.Tags);
在新列表或相同列表上展平列表
var posts = new List<Post>();
posts.Add(new Post { Title = "Foo", Tags = new[] { "Code" } } );
posts.Add(new Post { Title = "Foo1", Tags = new[] { "Code", "Productivity" } });
posts.Add(new Post { Title = "Foo2", Tags = new[] { "Miscellaneous" } });
var flattendPosts = new List<Post>();
foreach (var post in posts)
{
var tags = post.Tags.Select(tag => tag);
for (int i = 0; i < tags.Count(); i++)
{
flattendPosts.Add(new Post { Title = post.Title, Tag = post.Tags[i] });
}
}
flattendPosts.GroupBy(post => post.Tags);
var posts=newlist();
添加(newpost{Title=“Foo”,Tags=new[]{“Code”});
添加(新的Post{Title=“Foo1”,Tags=new[]{“Code”,“Productivity”});
添加(新的Post{Title=“Foo2”,Tags=new[]{“杂项”});
var flattedposts=新列表();
foreach(var post in post)
{
var tags=post.tags.Select(tag=>tag);
对于(int i=0;ipost.Tags);
如果您只想将其输出到控制台,那么您实际上不需要字典
:
var posts = Post.SeedPosts();
var tagGroups = posts
.SelectMany(p => p.Tags, (post, tag) => new{Tag = tag, post.Title})
.GroupBy(pair => pair.Tag);
foreach (var tagGroup in tagGroups)
{
Console.WriteLine(tagGroup.Key);
foreach (var pair in tagGroup)
{
Console.WriteLine(" " + pair.Title);
}
}
Console.ReadKey();
如果您只想将其输出到控制台,那么实际上不需要
字典
:
var posts = Post.SeedPosts();
var tagGroups = posts
.SelectMany(p => p.Tags, (post, tag) => new{Tag = tag, post.Title})
.GroupBy(pair => pair.Tag);
foreach (var tagGroup in tagGroups)
{
Console.WriteLine(tagGroup.Key);
foreach (var pair in tagGroup)
{
Console.WriteLine(" " + pair.Title);
}
}
Console.ReadKey();
可能的重复看看我的问题,我想你需要和我一样的我认为最好的方法是创建一个包含多个标记的元素副本,通过创建一个扁平的元素列表,每个元素只有一个标记。@AlexVoskresenskiy我正在尝试你的解决方案。@eranotzap请告诉我怎么做。可能的重复看看我的问题,我想你需要和我一样的东西。我想最好的方法是创建一个包含多个标记的元素副本,通过创建一个扁平的元素列表,每个元素只有一个标记。@AlexVoskresenskiy我现在正在尝试你的解决方案。@eranotzap请告诉我怎么做。