I am trying to populate IEnumerable<ResortSupplier> from an XML response using following LINQ Query: How do I populate/create IList<PerPricing> PricingDetail inside my LINQ query? There will be ONLY one item(PerPricing) in IList<PerPricing> in current scenario BUT it needs to be an IList for future options and to support some other functionality. I am getting error with below apporach.
var resortPricing =
xDoc.Elements("ResortPricingRS")
.Elements("ResortPricing")
.GroupBy(x => new
{
ID = x.Element(XName.Get("ResortId")).Value
}
)
.Select(x => new ResortSupplier
{
SupplierCode = x.Key.ID,
ResortProducts = x.Select(i => new Product
{
Code = i.Element("RoomCategory").Value,
Description = i.Element("CategoryDesc").Value,
Rating = (i.Elements("StarRatingCode").Any() ? i.Element("StarRatingCode").Value : ""),
PricingDetail = {
new PerPricing {
PricingType = "PerRoom",
GroupNumber = 1
Price = decimal.Parse(i.Elements("TotalPrice").Any() ? i.Element("TotalPrice").Value : "0"),
PricingError = (i.Elements("PricingError").Any() ? new Error { ErrorCode = i.Element("PricingError").Value } : null)
}
}
}
).ToList()
}
).OrderBy(x => x.SupplierCode);
Here are my objects:
[Serializable]
public class ResortSupplier
{
public string SupplierCode { get; set; }
public IList<Product> ResortProducts { get; set; }
}
[Serializable]
public class Product
{
public string Code { get; set; }
public string Description { get; set; }
public string Rating { get; set; }
public IList<PerPricing> PricingDetail { get; set; }
}
[Serializable]
public class PerPricing
{
public string PricingType { get; set; }
public int GroupNumber { get; set; }
public decimal? Price { get; set; }
public Error PricingError { get; set; }
}
Add
new []like so: