List 如何转换列表<;KeyValuePair<;字符串,对象>&燃气轮机;在.Net内核中创建类、模型

List 如何转换列表<;KeyValuePair<;字符串,对象>&燃气轮机;在.Net内核中创建类、模型,list,dictionary,generics,asp.net-core,.net-core,List,Dictionary,Generics,Asp.net Core,.net Core,我得到了响应列表,但我想将响应转换为列表 KeyValuePair键和ClassName属性都是相同的名称和类型 以编程方式转换响应的最有效方法是什么 我得到了答复 我的班级结构 public class TestModel { public string TaxablePersonCode { get; set; } public string LegalNameAsPerPan { get; set; } public string TradeName { ge

我得到了响应
列表
,但我想将响应转换为
列表

KeyValuePair
键和
ClassName
属性都是相同的名称和类型

以编程方式转换响应的最有效方法是什么

我得到了答复

我的班级结构

public class TestModel
{
    public string TaxablePersonCode { get; set; }
    public string LegalNameAsPerPan { get; set; }
    public string TradeName { get; set; }
    public string ConstitutionName { get; set; }
    public string ResidentialStatusName { get; set; }
    public string PrimaryMobileNo { get; set; }
    public string FlatOrOfficeNo { get; set; }
    public string TownOrCityOrDist { get; set; }
    public string Pincode { get; set; }
    public string StateName { get; set; }
    public string CountryName { get; set; }
    public string ContactName { get; set; }
    public string ContactDesignationName { get; set; }
    public string ContactMobile { get; set; }
    public string ContactEmail { get; set; }
}

 var listKeyValue = response.Select(x => x.Value).ToList();
 var data = JsonConvert.DeserializeObject<List<TestModel>>(listKeyValue);
公共类测试模型
{
公共字符串TaxablePersonCode{get;set;}
公共字符串LegalNameAsPerPan{get;set;}
公共字符串商标名{get;set;}
公共字符串ConstructionName{get;set;}
公共字符串ResidentialStatusName{get;set;}
公共字符串PrimaryMobileNo{get;set;}
公共字符串FlatOrOfficeNo{get;set;}
公共字符串townorcityrdist{get;set;}
公共字符串Pincode{get;set;}
公共字符串StateName{get;set;}
公共字符串CountryName{get;set;}
公共字符串ContactName{get;set;}
公共字符串ContactDesignationName{get;set;}
公共字符串ContactMobile{get;set;}
公共字符串ContactEmail{get;set;}
}
var listKeyValue=response.Select(x=>x.Value.ToList();
var data=JsonConvert.DeserializeObject(listKeyValue);

我唯一不确定的是何时为最终列表编制正确属性的索引,但您可以选择最适合自己的属性。每次使用
.Where()
可以确保获得正确的结果,但每次都会搜索列表,速度会慢得多。如果您的%100确定列表的顺序永远不会改变,您可以使用
[]
.ElementAt()
直接为所需元素的列表编制索引,从而获得一些性能。不管怎样,这就是你要找的

List<TestModel> myList = response.Select(x => new TestModel
{
    // Using Where
    TaxablePersonCode = x.Where(t => t.Key == "TaxablePersonCode").First().Value,
    // Using direct index
    LegalNameAsPerPan = x[1].Value,
    // Using ElementAt
    TradeName = x.ElementAt(2).Value,
    ...
});
List myList=response.Select(x=>newtestmodel
{
//在哪里使用
TaxablePersonCode=x.Where(t=>t.Key==“TaxablePersonCode”).First()值,
//使用直接索引
LegalNameAsPerPan=x[1]。值,
//使用ElementAt
商标名=x.ElementAt(2).Value,
...
});

希望有帮助

您在
jsonvert
类中得到了一些结果吗?这对你有用吗?如果没有,您可以尝试以下方法(如果JSON和字段的名称正确):


我看到您正在使用JSON,所以您可能应该正确地反序列化对象,我希望它看起来像这样:

List<TestModel> models = JsonConvert.DeserializeObject<List<TestModel>>(response);
// get all properties to populate
var properties = typeof(TestModel).GetProperties(BindingFlags.Public | BindingFlags.Instance);
List<TestModel> models = JsonConvert.DeserializeObject<List<TestModel>>(response);
outerList.ForEach(innerList => {
    TestModel result = new TestModel();
    innerList.ForEach(listItem => {
        result
            .GetType()
            .GetProperty(listItem.Key)
            ?.SetValue(result, listItem.Value); 
    });
});