类属性的局部变量

时间:2019-06-25 19:25:48

标签: linq class properties var

我已经使用linq创建了一个列表。不幸的是我想把它作为一个类的属性。如何将其转换为类的可访问属性?

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

1 个答案:

答案 0 :(得分:0)

您不能将匿名类型的实例作为函数或属性的返回值返回(根据我的最新信息。Google进行的快速搜索未表明到目前为止已更改)。这意味着您无法像使用new {VariableName1 = "123", VarName2 = "456"}创建那样列出匿名类型的列表。 您可以定义一个具有所需成员的类或结构,例如变量,索引,名称,路径。然后,当您构建列表时,而不是使用new {...}创建对象,而是创建一种命名类型,即:

在某处定义此:

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

相应地修改属性类型:

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 ###
}

然后:

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