我有下面的课程结构,
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和string的各种collection属性。
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"}
}
}
}
};
现在,我的目标是将收集结果拆分为各种实例,并根据数据类型对其进行分组。使用下面的代码,我可以拆分看跌期权,但是它给了我4个集合结果,但是应该给出2(2个代表双精度,2个代表字符串),请提出建议。谢谢!
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
之后添加一个selectMany
语句:
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);
这样,您将有两个组,它们已经用双键和字符串键分组了:
当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…
Mongo汇总 - javascript我的收藏中有以下文件{ "_id": ObjectId("54490b8104f7142f22ecc97f"), "title": "Sample1", "slug": "samplenews", "cat": …
如何在Wiremock中为JUNIT匹配精确的json - java我正在使用Wiremock在Spring启动应用程序中模拟Junit的REST服务。我的问题是,我无法匹配多个匹配模式。 Junit.javaStringValuePattern pattern = WireMock.matching(".*"); givenThat(post(urlEqualTo("/softwares…
运行此活动后,应用未响应 - java新的活动UI加载但没有响应,运行onStop()之后触发了Submit()带有复选框的列表视图受自定义适配器的约束。触摸“提交”按钮后,将触发一个意图,该意图将我带到HomeActivity,并且触发了onStop()方法,该方法随后返回调用Submit方法。所有提交方法都是在新线程下创建的,这会干扰UI。 包com.example.cadur.teache…
ObjectDisposedException:CancellationTokenSource已处置 - c#我开始在MacBook Pro上使用Xamarin Studio开发Xamarin.Forms。我构建了一个应用程序,其目的是查询PrestaShop网站,检索产品并显示它们。将应用程序部署到Android时,棉花糖以下的版本存在一些问题,但是我已解决了这些问题,因此在此不再赘述。将应用程序部署到iOS(模拟器)时,我仍然遇到严重问题。该应用程序运行,但是当…