Logic to get the Lot/SerialNo for distribution in SO_Invoice.

I have checked the videos and documentation about how to create a Sales Order Invoice and I have almost everything working as I want but I have problem making the distribution for those line which required it, specifically getting the Lot/SerialNo. I understand how it works but I do not know how can I to get this Lot/SerialNo value. Can somebody help me to get this done?

I am programming in C-Sharp but I accept any language suggestion, this is part of the code comming in the documentation:
For i As Integer = 1 To QuantityOrdered   

    ' Your Logic to get Lot/Serial number(s) goes here
    LotSerialNo = "F8561-3"    'How do I get LotSerialNo with code?

    ' Create distribution records
    retChk(soINVOICE.oLines.oDistribution.nSetKeyValue("InvoiceNo$", InvoiceNo), soINVOICE.oLines.oDistribution)
    retChk(soINVOICE.oLines.oDistribution.nSetKeyValue("LineKey$", LineKey), soINVOICE.oLines.oDistribution)
    retChk(soINVOICE.oLines.oDistribution.nSetKeyValue("LotSerialNo$", LotSerialNo), soINVOICE.oLines.oDistribution)
    retChk(soINVOICE.oLines.oDistribution.nSetKey(), soINVOICE.oLines.oDistribution)
    retChk(soINVOICE.oLines.oDistribution.nSetValue("TierType$", TierType), soINVOICE.oLines.oDistribution)
    retChk(soINVOICE.oLines.oDistribution.nSetValue("QuantityShipped", 1), soINVOICE.oLines.oDistribution)
    retChk(soINVOICE.oLines.oDistribution.nWrite(), soINVOICE.oLines.oDistribution)

Next i