I have working code in which I create a new var of type EnumerableRowCollection < XElement >. I have a new requirement where one of the XElements representing an Address must be conditionally included based on a Document Type value.
public class Taxes
{
public int DocumentType { get; set; }
private XElement BuildBodyXML()
{
// other stuff
Address billAddrObj = GetBillTo(dt);
Address buyerAddrObj = GetBuyerPrimary(dt);
var xBillTo = BuildAddress(billAddrObj, "BILL_TO");
var xBuyer = BuildAddress(buyerAddrObj, "BUYER_PRIMARY");
var INVOICE = from row in dt.AsEnumerable()
select new XElement(tcr + "INVOICE",
xBillTo, // This one needs to be conditionally included based on DocumentType
xBuyer,
// ... other elements ...
new XElement(tcr + "INVOICE_NUMBER", row.Field<string>("DOCNUMBR").Trim()));
// other stuff
return INVOICE;
}
public XElement BuildAddress(Address anAddress, string Name)
{
var xAddress = new XElement(tcr + Name);
// other stuff
return xAddress;
}
}
The Bill To XElement must be included conditionally based on the value of DocumentType. Can you help me achieve this?
UPDATE (Solution derived from answer by tinstaafl): I used the following code:
(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 16, 17, 18, 19, 20, 21, 22, 23, 24 }.Contains(DocumentType) ? xBillTo : null),