我的列表内容如下,
List<MyData> DICT_list = new List<MyData>()
{
new MyData{ Dictionay="Alpha", Dimension="Length", Classes=null},
new MyData{ Dictionay="Alpha", Dimension="Length", Classes="Process"},
new MyData{ Dictionay="Alpha", Dimension="breath", Classes="Activity"},
new MyData{ Dictionay="Alpha", Dimension="Height", Classes="Workflow"},
new MyData{ Dictionay="Beta", Dimension="Height", Classes=null},
new MyData{ Dictionay="Beta", Dimension="Height", Classes="Workflow"},
new MyData{ Dictionay="Beta", Dimension="Length", Classes="Workflow"}
};
我希望获得数据,例如如下,
Want to get All the Classes of Dimension=length in dictionary=ALPHA only...
非常感谢任何帮助。
---------有问题的更新-------
public struct MyData
{
public string Dictionary { get; set; }
public string Dimension { get; set; }
public string Classes { get; set; }
}
并执行如下,
var SelectedValue = DICT_list.Where(d => d.Dimension == "Length" && d.Dictionary == "Alpha").Select(d => d.Class);
foreach (var value in SelectedValue)
{
MessageBox.Show(value.ToString());
}
我在第MessageBox.show(value.ToString);
行收到跟踪错误
Object Reference not set to instance of an object
我的列表中包含了所需的元素,我已通过以下命令对其进行了测试,
string count = DICT_list.Count.ToString(); // Here count gives the value 14 which has the desired Dictionary and DImension name along with several classes which I need to fetch..
-------------------更新了问题---------
我想我知道为什么这个错误(如果我没错)是因为我的新维度的类的第一个值在List中是空的,我很抱歉错误的问题,现在通过测试了解,我可以做一些忽略空值并从列表中移动到下一步的东西。
答案 0 :(得分:3)
首先 - 只需按Dimension
和 Dictionay
过滤列表。第二 - 仅通过选择Classes
从结果中投射每个项目。这会给你IEnumerable<string>
:
var query = DICT_list.Where(d => d.Dimension == "Length" && d.Dictionay == "Aplha")
.Select(d => d.Classes);
或查询语法:
var query = from d in DICT_list
where d.Dimension == "Length" && d.Dictionay == "Aplha"
select d.Classes;
答案 1 :(得分:1)
@Sergey Berezovskiy给出的解决方案正常工作。 这是一个证明。
class Program
{
static void Main(string[] args)
{
List<MyData> DICT_list = new List<MyData>()
{
new MyData{ Dictionay="Aplha", Dimension="Length", Classes="Process"},
new MyData{ Dictionay="Aplha", Dimension="breath", Classes="Activity"},
new MyData{ Dictionay="Aplha", Dimension="Height", Classes="Workflow"},
new MyData{ Dictionay="Beta", Dimension="Height", Classes="Workflow"},
new MyData{ Dictionay="Beta", Dimension="Length", Classes="Workflow"}
};
var query = DICT_list.Where(d => d.Dimension == "Length" && d.Dictionay == "Aplha")
.Select(d => d.Classes);
foreach (var VARIABLE in query)
{
Console.WriteLine(VARIABLE.ToString());
}
Console.ReadLine();
}
}
输出:处理
后续问题的详尽答案:“我可以做一些忽略空值并从列表中移动到下一步的内容。”
此查询可能对您有所帮助:
var query = DICT_list.Where(d => d.Dimension == "Length" && d.Dictionay ==
"Aplha" && d.Classes != null).Select(d => d.Classes);