如何从每个内部List中获取所需的元素? [英] How to fetch desired elements out of every inner List?

查看:153
本文介绍了如何从每个内部List中获取所需的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储在List(对象列表)中的数据.

I have a data stored in List, which is List of objects.

我需要遍历每个内部列表,并检查对象是否满足条件,然后将其存储在其他列表中.

I need to traverse through every inner list and check if object satisfy the condition then store it in other List.

public class Data //class that actually holds data
{
    public string DataPart;
    public string Category;
    public Data (string dataPart, string category)
    {
        this.DataPart = dataPart;
        this.Category = category;
    }
}

存储数据的数据结构如下:

Data structure in which data is stored is like below:

Dictionary<int, List<Data>>

显示我当前解决方案的示例代码:

Sample code showing my current solution:

Dictionary<int, List<Data>> dataTbl = new Dictionary<int, List<Data>>();

//initializing the data structure
List<Data> lst1 = new List<Data>();
lst1.Add(new Data("data1OfLst1", "cat1"));
lst1.Add(new Data("data2OfLst1", "cat2"));
lst1.Add(new Data("data3Oflst1", "cat3"));
dataTbl.Add(1, lst1);
List<Data> lst2 = new List<Data>();
lst2.Add(new Data("data1OfLst2", "cat1"));
lst2.Add(new Data("data2OfLst2", "cat2"));
lst2.Add(new Data("data3Oflst2", "cat3"));
dataTbl.Add(2, lst2);

List<Data> cat1Data = new List<Data>();
foreach(List<Data> datList in dataTbl.Values)
{
    if(datList.Any( x => x.Category == "cat1"))
        cat1Data.Add(datList.Where(x => x.Category == "cat1").FirstOrDefault());
}

但是字典中的记录数和每个记录的List中的元素数将很大.因此,我正在寻找更好的解决方案.

But the number of records in Dictionary and the number of elements in every record's List will be a big number. Thus I'm looking for better solution.

请注意,Dictionary中的某些记录可能不包含任何满足条件的数据(此处为"cat1"检查).最终列表不应包含null

Note that it is possible that some of record in Dictionary may not contain any such data which satisfy the condition (here "cat1" check). final list should not contain null values

一般的假设(不是硬性验证)是,内部列表中应该只有一个(或没有)特定类别的条目,因此不会有包含多个"cat1"对象的内部列表.

It is general assumption (not a die-hard validation) that there should be only one (or no) entry of particular category in inner list, so there will be no inner lists which contain more than one objects with "cat1".

推荐答案

正如我已经在评论中告诉的那样,只要您不使用探查器来表示,您就不必为更快的代码而烦恼.如果,您甚至有一个性能问题,而如果,那么,如果,这是由您的代码或您没有想到的其他问题引起的.您会看到:如果有很多.

As I´ve already told in the comment, you shopuldn´t bother for faster code as long as you didn´t use a profiler to indicate if you even have a performance-problem and if so, if this is caused by your code or some other you don´t think of. You see: there are many if´s.

除此之外,我为您提供了一些更智能的代码-根本不会更快-如果有的话,但是更易于阅读和维护,这应该是您的主要目标否则只是在寻找纳秒级的时间.

Apart from this I have some smarter code for you, which won´t be much faster - if at all, but easier to read and thus to maintain, which should be your primary goal, everything else is just hunting for nano-seconds.

List<Data> cat1Data = new List<Data>();
foreach(List<Data> datList in dataTbl.Values)
{
    var el = datList.FirstOrDefault(x => x.Category == "cat1");
    if(el != null)
        cat1Data.Add(el);
}

FirstOrDefault将返回默认值(对于引用类型,null,对于结构类型,为结构默认值).实际上,您正在两次检查同一件事.

FirstOrDefault will return the default-value (null for reference-types, for struct-types the structs default-value). In fact you´re checking the same thing two times.

因此,您无需检查当前列表中是否有满足您条件的元素,然后再次选择该元素.而是直接搜索并添加(如果找到).

So you don´t need to check if there is an element satisfying your condition in the current list select this element again afterwards. Instead directly search for it and add it if found.

这篇关于如何从每个内部List中获取所需的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆