none
combining rows using vb.net RRS feed

  • Question

  • Hi All,

    Its urgent..i need to do this using vb.net.I have following table in oracle 9.

    Table Name:unit_note

    Note    |    Seq   |   Unit ID
    aaa          1           1
    bbb          2           1
    ccc          5           2
    ddd          4           3
    eee          5           3


    I need to fetch the data from oracle db and write it to .txt file by combining rows unit wise..

    Example:-
    In .txt file output would be like this:-

    Note         |    seq       |    Unit_ID
    aaa bbb           1 2              1
    ccc                    5               2
    ddd eee           4 5              3

    I m totally new to vb.net..Please help me in this. And  i need to do this task generically i.e by changing the table name ...the same logic shloud be applicable.
    Please help.

    Thanks,
    GSR
      
    Monday, September 29, 2014 3:04 PM

Answers

  • I would recommend that you handle this logic in Oracle by using a group by on Unit_ID and then concatenate the strings together.  If you want to do it in code then you can use LINQ to accomplish the same thing:

    var result = from d in data
                    group d by d.UnitId into g
                    select new MyData() { UnitId = g.Key, Note = String.Join(" ", g.Select(x => x.Note)), Seq = String.Join(" ", g.Select(y => y.Seq)) };
    
    //Assuming that MyData is your data you get from the DB and that it has columns/properties for UnitId, Note and Seq.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Marked as answer by gsr7 Tuesday, September 30, 2014 4:08 PM
    Monday, September 29, 2014 4:28 PM
    Moderator

All replies

  • I would recommend that you handle this logic in Oracle by using a group by on Unit_ID and then concatenate the strings together.  If you want to do it in code then you can use LINQ to accomplish the same thing:

    var result = from d in data
                    group d by d.UnitId into g
                    select new MyData() { UnitId = g.Key, Note = String.Join(" ", g.Select(x => x.Note)), Seq = String.Join(" ", g.Select(y => y.Seq)) };
    
    //Assuming that MyData is your data you get from the DB and that it has columns/properties for UnitId, Note and Seq.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Marked as answer by gsr7 Tuesday, September 30, 2014 4:08 PM
    Monday, September 29, 2014 4:28 PM
    Moderator
  • can u help me to do this using linq..
    Monday, September 29, 2014 4:34 PM
  • I posted the LINQ code as part of my post.  All you need to do is get the data from Oracle using whatever DB technology you're using.  Use EF model first if you need the simple, dirty solution.
    Monday, September 29, 2014 4:37 PM
    Moderator