如何在c#中将值连接到逗号分隔值

时间:2021-02-06 00:25:30

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. How to achieve that?

我正在阅读文本文件,我想根据条件获取值。首先我将采用FELLQ,其中CELLID = 639,ISMAINBCCH = YES,我现在已经完成了下一个任务,我必须以逗号分隔的方式连接FREQ值,其中CELLID = 639,ISMAINBCCH = NO,所以我想要的输出是24, 28,67。怎么实现呢?

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

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

Finally I did it like first I saved lines starting with ADD GTRX in to an array by using File. Readalllines and then used only that array to get concatenated string instead of storing entire text file and got some performance improvement.

最后我做到了,就像我首先使用File将以ADD GTRX开头的行保存到数组中。 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? If I use datatable and dataset rather than classes here will be a performance improvement?

如果我将包含数十万行的文本文件转换为xml,然后从xml文件而不是从文本文件中检索数据,它是否会提高性能?如果我使用数据表和数据集而不是类,这将是一个性能改进?

2 个解决方案

#1


0  

Create gtrxs collection to store Gtrx objects and read data from file in to 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. Once you have a list, you can simply use String.Join(",", freqValues) to build up CSV string.

创建gtrxs集合以存储Gtrx对象并将数据从文件读取到gtrxs。然后,您可以使用LINQ(ming需要使用System.Linq添加;)来查找符合您要求的Gtrx对象,并调用Select以获取Freq值列表。一旦有了列表,就可以使用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);

#2


0  

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")));

#1


0  

Create gtrxs collection to store Gtrx objects and read data from file in to 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. Once you have a list, you can simply use String.Join(",", freqValues) to build up CSV string.

创建gtrxs集合以存储Gtrx对象并将数据从文件读取到gtrxs。然后,您可以使用LINQ(ming需要使用System.Linq添加;)来查找符合您要求的Gtrx对象,并调用Select以获取Freq值列表。一旦有了列表,就可以使用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);

#2


0  

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")));