I am reading an XML document using the XDocument library and querying into a list with LINQ in C#. I am returning an anoymous list into an implicit var. The anynmous type is a simple structure. I am not sure what I am doing wrong but would like to return this into a List of a class I already defined but am getting an error converting this anonymous type into my custom class. Am I missing something obvious on this on. I do not want to use dynamic typing here.
public class CustomClass
{
public string id { get; set; }
public int fileName { get; set; }
}
XDocument optXML = XDocument.Load(pathName);
var optInput = (from item in optXML.Descendants("Group")
select new
{
id = (int)item.Attribute("ID"),
fileName = (string)item.Attribute("FileName")
}).ToList();
foreach (CustomClass item in optInput)
{
Console.WriteLine(item.id);
}