I’m having problems getting the master detail records to work with generics. My code is as follows
// Setup file engine with Master/Detail types
MasterDetailEngine<PurchaseOrderHeader, PurchaseOrderLine> engine = new MasterDetailEngine<PurchaseOrderHeader, PurchaseOrderLine>(CommonSelector.MasterIfBegins, "H");
// Init the Master/Detail
MasterDetails<PurchaseOrderHeader, PurchaseOrderLine> poRecs = new MasterDetails<PurchaseOrderHeader, PurchaseOrderLine>();
// Setup list for detail lines & populate with test data
List<PurchaseOrderLine> poLines = new List<PurchaseOrderLine>();
for (int i = 1; i <= 5; i++)
{
poLines.Add(new PurchaseOrderLine() { LineMarker = "L", LineNumber = i, ItemCode = "TestCode", Price = 12.34M, Quantity = i * 2, UOM = "EA" });
}
// Attach records to Master/Detail
poRecs.Master = new PurchaseOrderHeader() { LineMarker = "H", PayloadID = "3223232@ariba.acme.com", CustomerPO = "DO1234", ShipToAddressCode = "ShipToAddressCode", BillToAddressCode = "BillToAddressCode" };
poRecs.Details = poLines.ToArray();
// Write file
engine.WriteFile("SampleFile.txt", poRecs);
I get the following compilation error:
CS1502: The best overloaded method match for
FileHelpers.MasterDetail.MasterDetailEngine<ImportExport.PurchaseOrderHeader,ImportExport.PurchaseOrderLine>.WriteFile(string,System.Collections.Generic.IEnumerable<FileHelpers.MasterDetail.MasterDetails<ImportExport.PurchaseOrderHeader,ImportExport.PurchaseOrderLine>>)has some invalid arguments.
I’ve tried casting poRecs to IEnumerable but that still doesn’t work right.
Any suggestions would be greatly appreciated!
It looks like the call to
WriteFileexpects anIEnumerable<MasterDetails<PurchaseOrderHeader, PurchaseOrderLine>>, while you’re just passing in aMasterDetails<PurchaseOrderHeader, PurchaseOrderLine>.Of course you can’t cast a
MasterDetailsto anIEnumerablebecause those aren’t convertible. What you need to do is wrappoRecswith anIEnumerable. Try the following: