Search code examples
linqclasspropertiesvar

Local variable to property of class


I have created an a list using linq. Unfortunately i want to have it as a property of a class. How can i convert it to a accessable property of a class?

public class Component {

    // Property of class Component
    public string Komponentenart { get; set;}
    public int KomponentenID { get; set;}
    public string KomponentenArtikelnummer { get; set;}
    public var Variablen { get; set; }
}

public Component(string _filename)
{
    string componentFile = _filename;
try
        {
            StreamReader reader = new StreamReader(componentFile, Encoding.UTF8);
            XDocument xmlDoc = XDocument.Load(reader);


            var variablen = (from element in xmlDoc.Descendants("variable")
                             select new
                             {
                                 Variable = (string)element.Attribute("ID"),
                                 Index = (string)element.Attribute("index"),
                                 Name = (string)element.Attribute("name"),
                                 Path = (string)element.Attribute("path"),
                                 Interval = (string)element.Attribute("interval"),
                                 ConnectorId = (string)element.Attribute("connectorId"),
                                 Type = (string)element.Attribute("type"),
                                 Factor = (string)element.Attribute("factor"),
                                 MaxValue = (string)element.Attribute("maxvalue")
                             }
                ).ToList();
}

Solution

  • You can not return instances of anonymous types as return value of functions or properties (From my latest information. A quick google search yielded no indication that that's changed so far). Which means you can't make a list of an anonymous type like you are creating with new {VariableName1 = "123", VarName2 = "456"}. You could define a class or struct which has the members you need, such as Variable, Index, Name, Path. Then when you build your list, instead of creating an oject with new {...}, you create one of a named type, i.e.:

    Define this somewhere:

    class MyBunchOfVariables
    {
    public string Variable  ;
    public string Index         ;
    public string Name      ;
    public string Path      ;
    public string Interval  ;
    public string ConnectorId ;
    public string Type      ;
    public string Factor        ;
    public string MaxValue  ;
    }
    

    Modify the property type accordingly:

    public class Component
    {
    // Property of class Component
    public string Komponentenart { get; set;}
    public int KomponentenID { get; set;}
    public string KomponentenArtikelnummer { get; set;}
    public MyBunchOfVariables Variablen { get; set}; // ### CHANGED TYPE ###
    }
    

    And then:

    var variablen = (from element in xmlDoc.Descendants("variable")
                 select
                new MyBunchOfVariables
                    {
                     Variable       = (string)element.Attribute("ID"),
                     Index      = (string)element.Attribute("index"),
                     Name       = (string)element.Attribute("name"),
                     Path       = (string)element.Attribute("path"),
                     Interval       = (string)element.Attribute("interval"),
                     ConnectorId        = (string)element.Attribute("connectorId"),
                     Type       = (string)element.Attribute("type"),
                     Factor         = (string)element.Attribute("factor"),
                     MaxValue       = (string)element.Attribute("maxvalue")
                    }
                    ).ToList();