0

I'm having a old Visual FoxPro programm, which i need to rewrite in c#. There we used the cursors from VFP, to read .txt-files and load it into temporary cursors.

Looks for example like this in FoxPro: (mb5b is the mb5b-textfile)

SELECT werk,matnr,ALLTRIM(matnr)+ALLTRIM(werk) as matwerk,sum(zugang) as zugang,sum(abgang) as abgang INTO CURSOR mb5b_temp FROM mb5b GROUP BY werk,matnr

Those cursors dont exist in c#. (I didnt found anything like this.) So im creating a DataTable and while reading the file I insert it into the DataTable.

DataTable dt_mb5b_temp = new DataTable();
dt_mb5b_temp.Columns.Add("matnr");
dt_mb5b_temp.Columns.Add("werk");
dt_mb5b_temp.Columns.Add("matwerk");
dt_mb5b_temp.Columns.Add("zugang");
dt_mb5b_temp.Columns.Add("abgang");
while ((mb5bline = sr_mb5b.ReadLine()) != null)
{
    DataRow dr = dt_mb5b_temp.NewRow();
    string[] mb5b = mb5bline.Split(new Char[] { '|' });
    dr["matnr"] = mb5b[1].Trim();
    dr["werk"] = mb5b[2].Trim();
    dr["matwerk"] = mb5b[1].Trim() + mb5b[2].Trim();
    dr["zugang"] = mb5b[6].Trim();
    dr["abgang"] = mb5b[7].Trim();                        
}

I thought i may can work with the DataTable.Select() to use a select-statment as above, but it doesnt work ... And other solutions dont come to my mind at the moment :/

For sure i could also insert it into a DB - then use select, but i try to avoid this (Would need two extra tables, and i think those inserts and select will take a long time). Is there any possibility to get this working ?

Thanks!

If you need anymore Informations, please tell.

DatRid
  • 1,169
  • 2
  • 21
  • 46
  • Aren't you going a bit to far with the conversion from VFP to C# ? If the problem at hand is simply reading a txt file and parsing it into an object, just do it C# style ! If you like the querying style, you could give a shot to LINQ, check this [link](http://stackoverflow.com/questions/1271225/c-sharp-reading-a-file-line-by-line) – Simon Rapilly Sep 09 '13 at 10:08
  • @Simon Rapilly Could you give me an easy example ? I never used `LING`, and im stuck ... I think just a little example how you put it into a list and do it with a little sort would help. Just post it as answer and i accept it then! – DatRid Sep 10 '13 at 11:12
  • 1
    Well using LIN**Q** is just an example really, a simple for loop could do the trick, it all depends on how your txt file is constructed, could you detail the format of it in your question ? If you want to learn about LINQ, start with [this](http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b) it's quite good – Simon Rapilly Sep 10 '13 at 14:59

1 Answers1

1

look at this site. http://www.dotnetperls.com/readline

using System;
using System.Collections.Generic;
using System.IO;

class Program
{
    static void Main()
    {
    const string f = "TextFile1.txt";

    // 1
    // Declare new List.
    List<string> lines = new List<string>();

    // 2
    // Use using StreamReader for disposing.
    using (StreamReader r = new StreamReader(f))
    {
        // 3
        // Use while != null pattern for loop
        string line;
        while ((line = r.ReadLine()) != null)
        {
        // 4
        // Insert logic here.
        // ...
        // "line" is a line in the file. Add it to our List.
        lines.Add(line);
        }
    }

    // 5
    // Print out all the lines.
    foreach (string s in lines)
    {
        Console.WriteLine(s);
    }
    }
}

Output
    (Prints contents of TextFile1.txt)

This is a text file I created,
Just for this article.

group by ienum

    class Pet
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    // Uses method-based query syntax. 
    public static void GroupByEx1()
    {
        // Create a list of pets.
        List<Pet> pets =
            new List<Pet>{ new Pet { Name="Barley", Age=8 },
                           new Pet { Name="Boots", Age=4 },
                           new Pet { Name="Whiskers", Age=1 },
                           new Pet { Name="Daisy", Age=4 } };

        // Group the pets using Age as the key value  
        // and selecting only the pet's Name for each value.
        IEnumerable<IGrouping<int, string>> query =
            pets.GroupBy(pet => pet.Age, pet => pet.Name);

        // Iterate over each IGrouping in the collection. 
        foreach (IGrouping<int, string> petGroup in query)
        {
            // Print the key value of the IGrouping.
            Console.WriteLine(petGroup.Key);
            // Iterate over each value in the  
            // IGrouping and print the value. 
            foreach (string name in petGroup)
                Console.WriteLine("  {0}", name);
        }
    }

    /*
     This code produces the following output:

     8
       Barley
     4
       Boots
       Daisy
     1
       Whiskers
    */
Nick
  • 500
  • 8
  • 23
  • Thanks. I didnt got it working as i needed, but i also didnt spend more time on linq, so i did it with database inserts and selects ... As far as im total newb to Linq, this is the faster way now to get it working, but thanks for this, helping me to start the learning ;) – DatRid Sep 11 '13 at 07:58