Loop Section returns entire document

Post author
Jheminger

I am trying to return a specific section of the EDI file so I can loop it and make use of it.

Following a snippet I found I return an object however, it's the entire document and not the section I am interested in.

 

var x12Stream = getFile(theFile);

List<IEdiItem> ediItems = new List<IEdiItem>();
using (var ediReader = new X12Reader(x12Stream, LoadFactory, new X12ReaderSettings() { ContinueOnError = true }))
{
var myloop = new List<Loop_PO1_850>();
while (ediReader.Read())
{
var msg = ediReader.Item as TS850;
if (msg != null)
{
if(msg.IsPart)
{
myloop.AddRange(msg.PO1Loop);
}
else
{
msg.PO1Loop.AddRange(myloop);
ediItems.Add(msg);
myloop.Clear();
}
}
else
{
ediItems.Add(ediReader.Item);
}
}
}

Comments

5 comments

  • Comment author
    Admin
    • Edited

    Hello,

    Which loops do you need to batch together - on file level, interchange level, group level or transaction level ?

    For small files you can do:

    var x12Stream = getFile(theFile);
    List<Loop_PO1_850> ediItems;

    using (var ediReader = new X12Reader(x12Stream, TemplateFactory.FullTemplateFactory))
    ediItems = ediReader.ReadToEnd().OfType<TS850>().SelectMany(m => m.PO1Loop).ToList();

    ediItems will then hold all PO1 loops across all groups, interchanges and 850 transactions in the file.

    For large files, as I see you must have configured splitting by loop po1 in the template, you can do:

    var x12Stream = getFile(theFile);
    List<Loop_PO1_850> po1Loops = new List<Loop_PO1_850>();

    using (var ediReader = new X12Reader(ediStream, TemplateFactory.FullTemplateFactory))
    {
    while (ediReader.Read())
    if (ediReader.Item is TS850 po)
    po1Loops.AddRange(po.PO1Loop);
    }

    po1Loops holds all PO1 loops across all interchanges, groups and 850 transactions in the file.

     

     

    0
  • Comment author
    Jheminger

    This is helpful and moved me forward.

    Thank you for the fast response!

    Looking around the website I see many examples but, not an API documentation.

    Is there one available and can you give me a link?

    THX

    0
  • Comment author
    Admin

    API Reference

    0
  • Comment author
    Jheminger

    Thank you.

    When grabbing the CTT however I am getting an error ( same basic code )

    [code]

    List<Loop_CTT_850> ctt = new List<Loop_CTT_850>();

    using (var ediReader = new X12Reader(ediStream, LoadFactory, new X12ReaderSettings() { ContinueOnError = true }))
    {
    while (ediReader.Read())
    {
    var _ctt = ediReader.Item as TS850;
    if (_ctt != null)
    {
    ctt.AddRange(_ctt.CTTLoop);
    }
    }
    }

    [/code]

    I find CTTLoop in the 004010 template with Loop_CTT_850.

    I also tried just CTT.

    Thanks for any advice.

    If I can get a proof of concept to work we are definitely going to buy this tool.

    0
  • Comment author
    Admin

    List<Loop_CTT_850> ctt = new List<Loop_CTT_850>();

    using (var ediReader = new X12Reader(ediStream, LoadFactory, new X12ReaderSettings() { ContinueOnError = true }))
    {
    while (ediReader.Read())
    {
    var _ctt = ediReader.Item as TS850;
    if (_ctt != null)
    {
    ctt.Add(_ctt.CTTLoop);
    }
    }
    }

    0

Please sign in to leave a comment.