c# - 如何根据DataType组合 "collectionResult"

标签 c# .net linq

我有以下类结构,

 public class CollectionProperty
{
    public string Name { get; set; }

    public object Value { get; set; }
    public string DataType { get; set; }
}

public class Instance
{
    public string Name { get; set; }
    public List<CollectionProperty> CollectionProperties { get; set; }
}

public class CollectionResult
{
    public string Asset { get; set; }
    public List<Instance> Instances { get; set; }
}

我有以下集合结果,其中 2 个实例具有各种集合属性,具有 2 种数据类型 double 和字符串。

var collectionResult = new CollectionResult
        {
            Asset = "A1",
            Instances = new List<Instance>
            {
                new Instance
                {
                    Name = "Instance-1",
                    CollectionProperties = new List<CollectionProperty>
                    {
                        new CollectionProperty {Name = "N1", Value = 10, DataType = "Double"},
                        new CollectionProperty {Name = "N2", Value = "S1", DataType = "String"}
                    }
                },
                new Instance
                {
                    Name = "Instance-2",
                    CollectionProperties = new List<CollectionProperty>
                    {
                        new CollectionProperty {Name = "N1", Value = 20, DataType = "Double"},
                        new CollectionProperty {Name = "N2", Value = "S2", DataType = "String"}
                    }
                }
            }
        };

现在我的目标是将收集结果拆分为不同的实例,并根据数据类型对它们进行分组。使用下面的代码,我可以拆分 put,但它给了我 4 个集合结果,但它应该给出 2 个(2 个用于 double ,2 个用于字符串),请建议。谢谢!

enter image description here

var X = collectionResult.Instances
            .SelectMany(collectionInstance => collectionInstance.CollectionProperties,
                (collectionInstance, collectionProperty) =>
                    new
                    {
                        CollectionResult = new CollectionResult
                        {
                            Asset = collectionResult.Asset,
                            Instances = new List<Instance>
                            {
                                new Instance
                                {
                                    Name = collectionInstance.Name,
                                    CollectionProperties = new List<CollectionProperty>
                                    {
                                        collectionProperty
                                    }
                                }
                            },
                        },
                        Instance = collectionInstance.Name,
                        Property = collectionProperty.Name,
                        DataType = collectionProperty.DataType
                    });

最佳答案

您只需在 selectMany 之后添加一个 GroupBy 语句即可:

var X = collectionResult.Instances
        .SelectMany(collectionInstance => collectionInstance.CollectionProperties,
            (collectionInstance, collectionProperty) =>
                new
                {
                    CollectionResult = new CollectionResult
                    {
                        Asset = collectionResult.Asset,
                        Instances = new List<Instance>
                        {
                            new Instance
                            {
                                Name = collectionInstance.Name,
                                CollectionProperties = new List<CollectionProperty>
                                {
                                    collectionProperty
                                }
                            }
                        },
                    },
                    Instance = collectionInstance.Name,
                    Property = collectionProperty.Name,
                    DataType = collectionProperty.DataType
                }).GroupBy(c => c.DataType);

这样您将拥有两个组,它们已使用双键和字符串键分组:

enter image description here

关于c# - 如何根据DataType组合 "collectionResult",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60738027/

相关文章:

c# - 使用 Task.WaitAll() 时如何获取任务的返回值

c# - 从代码隐藏运行 javascript 函数

c# - LINQ 包含不区分大小写

c# - 使用其标签属性删除 ListViewItem 且不使用 foreach 循环

asp.net-mvc - LinqToDB : How to change connection string, 或在不使用项目配置的情况下设置它

c# - 如何在 Linq 中运行批量更新/删除查询?

c# - 如何使用工厂方法的返回值在启动类中添加单例

c# - list<T> 字段的平均计数

.net - Arraylist 使用 Array?

c# - 如何创建临时文件来缓存库的启动工作?