简体   繁体   English

如何在C#中将值连接成逗号分隔的值

[英]How to concatenate values in to a comma seperated values in c#

I am reading text file where I would like to get values based on condition. 我正在读取文本文件,我想根据条件获取值。 First I will take FREQ where CELLID = 639 and ISMAINBCCH=YES,that I have done now next task is I have to concatenate FREQ values in a comma separated way where CELLID=639 and ISMAINBCCH=NO, so the output I want is 24,28,67. 首先,我将使用CREID = 639和ISMAINBCCH = YES的FREQ,接下来要做的是我必须用逗号分隔的方式连接FREQ值,其中CELLID = 639和ISMAINBCCH = NO,所以我想要的输出是24, 28,67。 How to achieve that? 如何实现呢?

lines are 线是

ADD GCELL:CELLID=639, CELLNAME="NR_0702_07021_G1_A", MCC="424", MNC="02", LAC=6112, CI=7021, NCC=6, BCC=0, EXTTP=Normal_cell, IUOTP=Concentric_cell, ENIUO=ON, DBFREQBCCHIUO=Extra, FLEXMAIO=OFF, CSVSP=3, CSDSP=5, PSHPSP=4, PSLPSVP=6, BSPBCCHBLKS=1, BSPAGBLKSRES=4, BSPRACHBLKS=1, TYPE=GSM900_DCS1800, 
......................
.............

ADD GTRX:TRXID=0, TRXNAME="M_RAK_JeerExch_G_1879_18791_A-0", FREQ=81, TRXNO=0, CELLID=639, IDTYPE=BYID, ISMAINBCCH=YES, ISTMPTRX=NO, GTRXGROUPID=2556;
ADD GTRX:TRXID=1, TRXNAME="M_RAK_JeerExch_G_1879_18791_A-1", FREQ=24, TRXNO=1, CELLID=639, IDTYPE=BYID, ISMAINBCCH=NO, ISTMPTRX=NO, GTRXGROUPID=2556;
ADD GTRX:TRXID=5, TRXNAME="M_RAK_JeerExch_G_1879_18791_A-2", FREQ=28, TRXNO=2, CELLID=639, IDTYPE=BYID, ISMAINBCCH=NO, ISTMPTRX=NO, GTRXGROUPID=2556;
ADD GTRX:TRXID=6, TRXNAME="M_RAK_JeerExch_G_1879_18791_A-3", FREQ=67, TRXNO=3, CELLID=639, IDTYPE=BYID, ISMAINBCCH=NO, ISTMPTRX=NO, GTRXGROUPID=2556;

Update 更新

I am getting values like shown below 我正在获得如下所示的值

using (StreamReader sr = File.OpenText(filename))
{
    while ((s = sr.ReadLine()) != null)
    {
        if (s.Contains("ADD GCELL:"))
        {
            var gtrx = new Gtrx
            {
                CellId = int.Parse(PullValue(s, "CELLID")),
                Freq = int.Parse(PullValue(s, "FREQ")),
                TrxNo = int.Parse(PullValue(s, "TRXNO")),
                IsMainBcch = PullValue(s, "ISMAINBCCH").ToUpper() == "YES",
                TrxName = PullValue(s, "TRXNAME"),

            };

        }
    }

UPDATE UPDATE

I used facade concept, but now it is taking lot of time. 我使用了外观概念,但是现在却花费了很多时间。 I am not sure whether I am using any bad logic two times I am iterating text file one for getting regular values and other for getting concatenated values 我不确定两次是否使用任何错误逻辑来迭代文本文件,一次用于获取常规值,另一次用于获取串联值

 private class Gtrx
    {
        public int Freq { get; set; }
        public int TrxNo { get; set; }
        public string TrxName { get; set; }
        public int CellId { get; set; }
        public bool IsMainBcch { get; set; }
    }

    private class Gcell
    {
        public int CellId { get; set; }
        public string CellName { get; set; }
        public string Mcc { get; set; }
        public int Lac { get; set; }
        public int Ci { get; set; }
    }
    private class GcellGtrx
    {
        public Gcell Gcell { get; set; }
        public Gtrx Gtrx { get; set; }
    }

using (var sr = new StringReader(data))
{
    string line = sr.ReadLine();
    while (line != null)
    {
        line = line.Trim();
        if (line.StartsWith("ADD GCELL:"))
        {
            var gcell = new Gcell
            {
                CellId = int.Parse(PullValue(line, "CELLID")),
                CellName = PullValue(line, "CELLNAME"),
                Ci = int.Parse(PullValue(line, "CI")),
                Lac = int.Parse(PullValue(line, "LAC")),
                Mcc = PullValue(line, "MCC")
            };
            var gcellGtrx = new GcellGtrx();
            gcellGtrx.Gcell = gcell;
            _dictionary.Add(gcell.CellId, gcellGtrx);
        }
        if (line.StartsWith("ADD GTRX:"))
        {
            var gtrx = new Gtrx
            {
                CellId = int.Parse(PullValue(line, "CELLID")),
                Freq = int.Parse(PullValue(line, "FREQ")),
                TrxNo = int.Parse(PullValue(line, "TRXNO")),
                IsMainBcch = PullValue(line, "ISMAINBCCH").ToUpper() == "YES",
DEFINED_TCH_FRQ = null,
                TrxName = PullValue(line, "TRXNAME")
            };

           if (!intarr.Contains(gtrx.CellId))
                            {

                                if (!_dictionary.ContainsKey(gtrx.CellId))
                                {
                                    // No GCell record for this id. Do something!
                                    continue;
                                }
                                intarr.Add(gtrx.CellId);
                                string results = string.Empty;

                                    var result = String.Join(",",
        from ss in File.ReadLines(filename)
        where ss.Contains("ADD GTRX:")
        where int.Parse(PullValue(ss, "CELLID")) == gtrx.CellId
        where PullValue(ss, "ISMAINBCCH").ToUpper() != "YES"
        select int.Parse(PullValue(ss, "FREQ")));
                                    results = result;


                                var gtrxnew = new Gtrx
                                {
                                    DEFINED_TCH_FRQ = results
                                };

                                _dictionary[gtrx.CellId].Gtrx = gtrx;
        }
        line = sr.ReadLine();
    }
}

UPDATE UPDATE

Finally I did it like first I saved lines starting with ADD GTRX in to an array by using File. 最终,我做到了,就像首先使用File将ADD GTRX开头的行保存到数组中一样。 Readalllines and then used only that array to get concatenated string instead of storing entire text file and got some performance improvement. Readalllines,然后仅使用该数组来获取串联的字符串,而不是存储整个文本文件并获得了一些性能改进。

If I convert my Text files that contain hundreds of thousands of lines each into xml and then retrieve data from xml file rather from text file, will it make any performance improvement? 如果我将每个包含成千上万行的文本文件转换为xml,然后从xml文件而不是从文本文件中检索数据,是否会改善性能? If I use datatable and dataset rather than classes here will be a performance improvement? 如果我使用数据表和数据集而不是类,性能会有所改善吗?

Create gtrxs collection to store Gtrx objects and read data from file in to gtrxs . 创建gtrxs集合以存储Gtrx对象,并将文件中的数据读取到gtrxs Then you can use LINQ (ming need to add using System.Linq; ) to find Gtrx objects that are matching your requirements and call Select to get list of Freq values. 然后,您可以使用LINQ(可能需要using System.Linq;进行添加)来找到与您的需求相匹配的Gtrx对象,然后调用Select以获取Freq值列表。 Once you have a list, you can simply use String.Join(",", freqValues) to build up CSV string. 有了列表后,您可以简单地使用String.Join(",", freqValues)建立CSV字符串。

var gtrxs = new List<Gtrx>();

using (StreamReader sr = File.OpenText(filename))
{
    while ((s = sr.ReadLine()) != null)
    {


        if (s.Contains("ADD GCELL:"))
        {

            var gtrx = new Gtrx
                                {
                                    CellId = int.Parse(PullValue(s, "CELLID")),
                                    Freq = int.Parse(PullValue(s, "FREQ")),
                                    TrxNo = int.Parse(PullValue(s, "TRXNO")),
                                    IsMainBcch = PullValue(s, "ISMAINBCCH").ToUpper() == "YES",
                                    TrxName = PullValue(s, "TRXNAME"),

                                };

            gtrxs.Add(gtrx);
        }
    }
}

IEnumerable<int> freqValues = gtrxs.Where(x => x.CellId == 639 && x.IsMainBcch == false).Select(x => x.Freq);
string result = String.Join(",", freqValues);

Does this work for you? 这对您有用吗?

var result = String.Join(",", 
    from s in File.ReadAllLines(filename)
    where s.Contains("ADD GCELL:")
    where int.Parse(PullValue(s, "CELLID")) == 639
    where PullValue(s, "ISMAINBCCH").ToUpper() != "YES"
    select int.Parse(PullValue(s, "FREQ")));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM