过滤条件

时间:2016-10-26 20:26:30

标签: c# visual-studio arraylist

我有一个包含多个项目的ArrayList,每个人都是一个字符串除以逗号“loglogs”,三个第一项是本地化(Destin,lat和long)。我需要在按钮工具提示或文本编程中根据其本地化(基于这三个参数)在按钮中插入这些loglog的字符串。我有所有的按钮创建,但我必须添加字符串,但有更多的loglog而不是按钮所以......

我需要将ArrayList“过滤”到另一个ArrayList中,根据这三个初始坐标对其进行过滤,我想创建另一个ArrayList,但是在arrayList的三个第一个元素中附加相同的字符串。这样我就会将“loglogs”组合成另一个“loglogsCondensed”,所有“本地化”都是唯一的,所以我可以将这部分添加到我的按钮和索引创建中。

        foreach (String log in logslogs)
        {
            String[] colContent = log.Split(',');   //splited the content with commas
            Loglog log = new Loglog();  //Loglog is a class of logs with information in specific columns
            log.Destin = colContent[0];
            log.Lat = Convert.ToChar(colContent[1]);
            log.Long = colContent[2];                
            log.Barcode = colContent[6];
            log.Source = colContent[7];                
            log.SampleName = colContent[9];                
            AllLogs.Add(log);

我需要将带有1000个memebers的logslogs传递给一个包含较少项目的ArrayList,其中基于这三个第一项的具有相同位置的那些将作为一个项目附加。

如果你知道如何正确编码(不是我的情况),这很容易。一千个感谢只有阅读这个,更多的是对那些试图帮助的人。

最佳,

1 个答案:

答案 0 :(得分:0)

我有解决方案!,可能不会赢得任何清洁比赛,但它做我需要的!我创建了一个索引来过滤比较三个坐标的项目:Destin,Long和Lat。如果它们是相同的,我删除最后一项并将附加的行放在最后的位置,依此类推......

    int c = 0;  //Just to go the first time
    //We create an index to compare the former with the "actual"     
    //log in every loop of the "foreach"

        String IndiceDestin0 = string.Empty;
        String IndiceLat0 = string.Empty;
        String IndiceLong0 = string.Empty;

        String IndiceDestin1;
        String IndiceLat1;
        String IndiceLong1;          

        foreach (String log in logslogs)
        {
            String[] LongContent = log.Split(',');
            Loglog log = new Loglog();
            log.Destin = LongContent[0];
            log.Lat = Convert.ToChar(LongContent[1]);
            log.Long = LongContent[2];                
            log.Barcode = LongContent[6];
            log.Source = LongContent[7];
            log.DestDestinBarcode = LongContent[8];
            log.SampleName = LongContent[9];                
            AllLogs.Add(log);

            //This only works once, the first time because we don't have a "former" data to compare we have to bypass the comparison
            if (c == 0) 
            {
                IndiceDestin0 = LongContent[0];
                IndiceLat0 = LongContent[1];
                IndiceLong0 = LongContent[2];
                c++;
            }
            else
            {
                IndiceDestin1 = LongContent[0];
                IndiceLat1 = LongContent[1];
                IndiceLong1 = LongContent[2];

                if (IndiceDestin0.Equals(IndiceDestin1) && IndiceLat0.Equals(IndiceLat1) && IndiceLong0.Equals(IndiceLong1))
                {                        
                    int last = logsToButtons.Count - 1;
                    string oldLog = logsToButtons[last].ToString();
                    string appendedLog = oldLog + log;
                    //We remove the last "single" log to add the aggregated log
                    logsToButtons.RemoveAt(last);

                    logsToButtons.Add(appendedLog);                        
                }
                else
                {
                    logsToButtons.Add(log);
                }

                IndiceDestin0 = IndiceDestin1;
                IndiceLat0 = IndiceLat1;
                IndiceLong0 = IndiceLong1;
                c++;
            }                               

        }

我得到了一个较短版本的阵列,但将具有相同坐标的那些附加在一起,谢谢大家的帮助,我知道它很麻烦,但它有效!

最佳,