我有一个属性列表,它将是动态的,并且可能在每个请求中频繁更改。
var dynamicFields = new List<string>{ "UserName", "UserEmail" };
我有另一个类别列表,其中包含所有动态字段和其他字段。
var staticFields = new List<StaticFields>
{
new StaticFields {UserName = "Chandra", UserDepartment = "IT", UserCity = "Bangalore", UserEmail = "abc@gmail.com"},
new StaticFields {UserName = "Sekar", UserDepartment = "CSE", UserCity = "Bangalore", UserEmail = "xyz@gmail.com"},
new StaticFields {UserName = "Dilip", UserDepartment = "IT", UserCity = "Bangalore", UserEmail = "cba@gmail.com"}
};
public class StaticFields
{
public string UserName {get; set;}
public string UserDepartment {get; set;}
public string UserCity {get; set;}
public string UserEmail {get; set;}
//etc..
}
--
我需要仅选择在dynamicFields列表中的字段。
那么我怎样才能在C#中实现这个功能?可以使用for循环或者使用LINQ。
编辑:
目的是只显示所选列的内容,我正在使用SP从数据库中提取所有数据。它是来自数据库的遗留代码,因此我无法访问修改DB存储过程的任何内容。
我已经尝试了以下JS代码。
var i;
var properties = [
'UserName',
'UserEmail'
];
for (i = 0; i < properties.length; i += 1) {
document.writeln(properties[i] + ': ' + another_object[properties[i]]);
}
我需要将这段代码转换成C#语言
result[i]["UserName"]
的东西中访问吗? - Yuliam Chandra