linq c# 对象 ToDictionary
linq c# object ToDictionary
我正尝试在 linq 中编写查询,我希望 x.key 将其添加到我的字典中,但它没有显示。不确定如何进一步进行。我查看了其他线程,但找不到与此对象类似的示例。我是新手,非常感谢任何帮助
if (emp.something != null)
{
foreach (var item in emp.something)
{
if (item.Value.someId.AllowMultiple.Equals(false))
{
var ff = (singleObject) item.Value;
if (ff.Value != null)
{
Dict.Add((int)ff.Value, item.Key);
}
}
}
}
emp.something?.Where(x => x.Value.someId.AllowMultiple.Equals(false))
.Select(y => (singleObject) y.Value)
.ToDictionary(y => y.Value, x.
您的 ToDictionary
调用与之前 Select
中的任何 returns 一起工作。由于在该方法中,您 return Value
属性,ToDictionary
处理的集合是 Value
值的集合。换句话说,它对前面命令中的 x
值一无所知。您必须将其包含在投影中才能使用它 "downstream"。
相当于你的循环似乎是:
emp.something?.Where(item => item.Value.someId.AllowMultiple.Equals(false)
.Select(item => new {item, ff = (singleObject) item.Value}
.Where(x => x.ff.Value != null)
.ToDictionary(x => (int)x.ff.Value, x => x.item.Key);
但是强制转换和嵌套 Values
让人感到困惑。如果你的循环有效,我会坚持下去——它会更容易调试,而等效的 Linq 查询更难理解(在我看来)。
我正尝试在 linq 中编写查询,我希望 x.key 将其添加到我的字典中,但它没有显示。不确定如何进一步进行。我查看了其他线程,但找不到与此对象类似的示例。我是新手,非常感谢任何帮助
if (emp.something != null)
{
foreach (var item in emp.something)
{
if (item.Value.someId.AllowMultiple.Equals(false))
{
var ff = (singleObject) item.Value;
if (ff.Value != null)
{
Dict.Add((int)ff.Value, item.Key);
}
}
}
}
emp.something?.Where(x => x.Value.someId.AllowMultiple.Equals(false))
.Select(y => (singleObject) y.Value)
.ToDictionary(y => y.Value, x.
您的 ToDictionary
调用与之前 Select
中的任何 returns 一起工作。由于在该方法中,您 return Value
属性,ToDictionary
处理的集合是 Value
值的集合。换句话说,它对前面命令中的 x
值一无所知。您必须将其包含在投影中才能使用它 "downstream"。
相当于你的循环似乎是:
emp.something?.Where(item => item.Value.someId.AllowMultiple.Equals(false)
.Select(item => new {item, ff = (singleObject) item.Value}
.Where(x => x.ff.Value != null)
.ToDictionary(x => (int)x.ff.Value, x => x.item.Key);
但是强制转换和嵌套 Values
让人感到困惑。如果你的循环有效,我会坚持下去——它会更容易调试,而等效的 Linq 查询更难理解(在我看来)。