c# - 扁平化关键集合项目的集合 Linq

标签 c# linq

我将如何使用 Linq 展开下面的结构?

enter image description here

我正在查看以下结果:

Date      Ids
1/1/2011  1
1/1/2011  2
1/1/2011  3
1/1/2012  3

etc..

我试过 SelectMany,但我做错了。此示例的一些片段:

public class DTO
{
    public DateTime Date { get; set; }
    public List<int> Ids { get; set; }
}

作为

var dataSet = new List<DTO>{
    new DTO {Date = new DateTime(2011,1,1), Ids = new List<int>{1,2,3} },
    new DTO {Date = new DateTime(2012,1,1), Ids = new List<int>{3,4,5} },
    new DTO {Date = new DateTime(2013,1,1), Ids = new List<int>{5,6,7} }
    };

最佳答案

您正在寻找 SelectMany

var result = dataSet.SelectMany(x => x.Ids, (dto,id) => {
    return new { date = dto.Date,id};
});

实例:http://rextester.com/GHY67873

关于c# - 扁平化关键集合项目的集合 Linq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43846324/

相关文章:

c# - 如何安装最新的服务栈开源dll

c# - 如何跳过最后 2 条记录并使用 linq 获取所有其他记录?

c# - LINQ to XML - 尝试通过元素的属性值选择元素列表

c# - 在 linq foreach 中调用方法 - 有多少开销?

c# - “Unexpected PROPERTYELEMENT in parse rule Element” C#错误

c# MVVM在选择组合框后更新Datagrid

c# - 我应该在哪里插入自定义 DefaultContractResolver JSON.NET?

c# - PostSharp:使用 OnMethodInvocationAspect 时删除了自定义属性

c# - Linq 获取指定的 child 和他们的 parent

c# - 尝试确定两个 List<T> 中的公共(public)元素时出现问题