简体   繁体   English

如何将var转换为List c#

[英]how can convert var to List c#

I would like to convert var to list value . 我想将var转换为list值。 I am getting 'myobj' var value. 我正在获取“ myobj”变量值。 Now how can i convert to CandidateResume list ?. 现在如何转换为CandidateResume列表?

 JavaScriptSerializer jsSerializer = new JavaScriptSerializer() { MaxJsonLength = 86753090 };
 var myobj = jsSerializer.Deserialize<List<List<CandidateResume>>>(description);

My CandidateResume class like this 我的CandidateResume类是这样

public class CandidateResume
        {

            public string name { get; set; }
            public string url { get; set; }
            public string summary { get; set; }
            public string role { get; set; }
            public string compensation { get; set; }
            public string education { get; set; }
            public string expertise { get; set; }
            public string years { get; set; }
            public string relocation { get; set; }
            public string resume { get; set; }
            public string resumeExtension { get; set; }
            public string resumeMimeType { get; set; }

        }

Please see this current item value in screenshot 请在屏幕截图中查看此当前项目值 在此处输入图片说明

How can i catch item value ? 如何获取商品价值?

     foreach (var item in myobj)
                    {
 WebScrappingCandidate webScrappingCandidate = new WebScrappingCandidate();

}

Deserialize<T> returns a T ( MSDN ), which means myObj is already of type: List<List<CandidateResume>> . Deserialize<T>返回一个TMSDN ),这意味着myObj已经是类型: List<List<CandidateResume>>

Remember, var is not a type, it just uses type inference to be shorthand for: "Be whatever type I'm being assigned to". 记住, var 不是类型,它只是使用类型推断来简化:“成为我要分配给的任何类型”。

To "flatten" this list, you could just use SelectMany : 要“拉平”此列表,您可以使用SelectMany

foreach (CandidateResume resume in myObj.SelectMany(i => i))
{

}

Of course, there are other methods as well, but the crux of the answer is that you already have a list of candidate resumes. 当然,还有其他方法,但是答案的关键是您已经有了候选人简历的列表。 No conversion required. 无需转换。

You already have a list, the Deserialize method returns a typed result: 您已经有了一个列表, Deserialize方法返回一个键入的结果:

List<List<CandidateResume>> myobj =
  jsSerializer.Deserialize<List<List<CandidateResume>>>(description);

When you loop through that, each item is a list: 当您遍历时,每个项目都是一个列表:

foreach (List<CandidateResume> item in myobj) {
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM