【问题标题】:Local variable to property of class类属性的局部变量
【发布时间】:2019-11-07 16:33:21
【问题描述】:

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

【问题讨论】:

    标签: linq class properties var


    【解决方案1】:

    您不能将匿名类型的实例作为函数或属性的返回值返回(根据我的最新信息。快速的 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();
    

    【讨论】:

    • 对这个话题感到精神沮丧,我自己找到了这个解决方案。当你的信息到达时,我还在编程。该解决方案完全按预期工作。如果你知道怎么做,这似乎很容易。感谢您的帮助!
    猜你喜欢
    • 2013-09-23
    • 1970-01-01
    • 2012-09-17
    • 2013-06-08
    • 2013-04-29
    • 2011-05-28
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多