0

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);
        }

4 Answers 4

4

Instead of selecting a new{} select a new CustomClass():

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 CustomClass()
                    {
                        id = (int)item.Attribute("ID"),
                        fileName = (string)item.Attribute("FileName")
                    }).ToList();
foreach (CustomClass item in optInput)
        {
            Console.WriteLine(item.id);
        }
Sign up to request clarification or add additional context in comments.

Comments

2

I think you want to project into CustomClass rather than an anonymous object

List<CustomClass> optInput = (from item in optXML.Descendants("Group")
                select new CustomClass 
                {
                    id = (int)item.Attribute("ID"),
                    fileName = (string)item.Attribute("FileName")
                }).ToList();

There's no specific reason to change var in your code to List<CustomClass> - ive just done it to demonstrate to you what you're actually getting.

Comments

1

You can use var instead of custom type, it will be the type generated by the compiler in your projection use new.

foreach (var item in optInput)
{
     Console.WriteLine(item.id);
}

or you can create object of your class i.e CustomClass instead of using anonymous type.

  var optInput = (from item in optXML.Descendants("Group")
                select new CustomClass 
                {
                    id = (int)item.Attribute("ID"),
                    fileName = (string)item.Attribute("FileName")
                }).ToList();

1 Comment

Is there a way to import directly into the custom class like List<CustomClass> = (from item in optXML.Descendants("Group") select new { id = (int)item.Attribute("ID"), fileName = (string)item.Attribute("FileName") }).ToList();
1

You're getting this error because the anonymous type you select inside your query is NOT CustomClass. You can simply select Customclass itself like so

    select new CustomClass { id = , fileName = }

or use the anomymous type like so inside the foreach

    foreach (var item in optInput) 
    { 
        Console.WriteLine(item.id); 
    }       

and you'll get full intellisense for it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.