从视图到列表的数据

时间:2017-04-11 06:19:26

标签: c#

我有一个视图,它返回以下行的数据 - Id,SourceStatus,StatusId,ActivePre,Runtime。 在c#中,我创建了一个类,并编写了一个参数化查询来获取数据。

public class TestClass
{

    public int Id { get; set; }

    public bool? SourceStatus { get; set; }

    public int? StatusId{ get; set; }

    public int? ActivePre { get; set; }

    public string Runtime { get; set; }
}


string mydata= "SELECT * FROM view1 WHERE Id = {0}";
deviceData.TestList = dataContext.ExecuteQuery<TestClass>(mydata, deviceId).ToList();

在deviceData.TestList中,我得到了完美的数据。我的挑战是,我需要将这些数据放在另一个列表中 - HeaderData

public class HeaderData
{
    public STRING Name { get; set; }
    public STRING Value { get; set; }

}

这样我的列表看起来 - {Name:Assetid,value:1,Name:SourceStatus,value:false}。我目前正在这样做。有点我明白这是一种可怕的方式。我怎么能做得更好?

      deviceData.HeaderData= new List<HeaderData>();
      foreach( var v in deviceData.TestList)
            {
                deviceData.HeaderData.Add(new HeaderData() { Name = "Id" , Value = v.ActivePreset});
                deviceData.HeaderData.Add(new DeviceHeaderData() { Name = "SourceStatus", Value = v.SourceStatus });
                deviceData.HeaderEntries.Add(new DeviceHeaderData() { Name = "Runtime", Value = v.Runtime });
            }

2 个答案:

答案 0 :(得分:0)

这样的事情怎么样?

// Get property array
var properties = GetProperties(some_object);

foreach (var p in properties)
{
    string name = p.Name;
    var value = p.GetValue(some_object, null);
}

private static PropertyInfo[] GetProperties(object obj)
{
    return obj.GetType().GetProperties();
}

答案 1 :(得分:0)

这样的事情可能是什么?

  C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\Microsoft.Cpp.Platform
.targets(44,5): error MSB8020: The builds tools for v110_xp (Platform Toolset =
 'v110_xp') cannot be found. To build using the v110_xp build tools, either cli
ck the Project menu or right-click the solution, and then select "Update VC++ P
rojects...". Install v110_xp to build using the v110_xp build tools. [c:\\xxx.vcxproj]

没有测试这段代码,这只是一个例子,为了使这段代码有效,你必须添加名称属性&amp;&amp; value property to。

  

TestList类