无法从内部 dictionaries/lists 检索对象
Cannot retrieve object from inner dictionaries/lists
我目前正在尝试获取深度嵌套在字典和列表中的对象。
该对象是 ProductInfo 字典中的 ProductPrice 对象。
结构是:
Debug info
我尝试了以下 LINQ,但这只是 returns 我的列表(就在对象之前)
var Lis = productInfo
.Where(x => x.Key == "Prices")
.ToDictionary(x => x.Key, x => x.Value)
.Where(x => x.Key == "Prices")
.Select(x => x.Value)
.ToList()
.First();
productinfo class 是一个 类型的字典。我不知道为什么 first() 命令仍然给我完整的列表而不是对象本身......关于如何迭代第一个字典的任何想法找到价格字典然后迭代那个。检索列表并最终得到它的第一个元素?
我的linq结果是她:
Linq variable first()
更新产品信息Class:
using System.Collections.Generic;
namespace Dynamicweb.Ecommerce.Integration
{
public class ProductInfo : Dictionary<string, object>
{
public ProductInfo();
}
}
很难确定,我无法测试这个因为我没有你 classes 或数据所以我只能关闭你的调试屏幕截图
var prices = productInfo["Prices"];
价格似乎是 List
的 something.something.ProductPrice。但看起来你想要第一个。所以它只是
var prices = productInfo["Prices"].First();
我不明白你为什么需要那个极其复杂的 LINQ 链。或者您是否打算从 ProductPrice class
中提取一些内容
您无需重复任何内容。您的 productInfo
是一个对象字典。您需要将字典访问的值转换为适当的类型 (List<ProductPrice>
) 并对其进行操作。在这种情况下,您可以调用 First()
来获取列表中的第一项。
var query = ((List<ProductPrice>)productInfo["Prices"]).First();
如果字典中可能没有“Prices”,或者它不是预期的类型或可能为空,您可以检查一下。
var query = productInfo.TryGetValue("Prices", out var v) && v is IEnumerable<ProductPrice> e
? e.FirstOrDefault()
: default;
我目前正在尝试获取深度嵌套在字典和列表中的对象。
该对象是 ProductInfo 字典中的 ProductPrice 对象。
结构是:
Debug info
我尝试了以下 LINQ,但这只是 returns 我的列表(就在对象之前)
var Lis = productInfo
.Where(x => x.Key == "Prices")
.ToDictionary(x => x.Key, x => x.Value)
.Where(x => x.Key == "Prices")
.Select(x => x.Value)
.ToList()
.First();
productinfo class 是一个
我的linq结果是她:
Linq variable first()
更新产品信息Class:
using System.Collections.Generic;
namespace Dynamicweb.Ecommerce.Integration
{
public class ProductInfo : Dictionary<string, object>
{
public ProductInfo();
}
}
很难确定,我无法测试这个因为我没有你 classes 或数据所以我只能关闭你的调试屏幕截图
var prices = productInfo["Prices"];
价格似乎是 List
的 something.something.ProductPrice。但看起来你想要第一个。所以它只是
var prices = productInfo["Prices"].First();
我不明白你为什么需要那个极其复杂的 LINQ 链。或者您是否打算从 ProductPrice class
中提取一些内容您无需重复任何内容。您的 productInfo
是一个对象字典。您需要将字典访问的值转换为适当的类型 (List<ProductPrice>
) 并对其进行操作。在这种情况下,您可以调用 First()
来获取列表中的第一项。
var query = ((List<ProductPrice>)productInfo["Prices"]).First();
如果字典中可能没有“Prices”,或者它不是预期的类型或可能为空,您可以检查一下。
var query = productInfo.TryGetValue("Prices", out var v) && v is IEnumerable<ProductPrice> e
? e.FirstOrDefault()
: default;