There is such a method:

public List<string> GetAccountsYield() { var client = new WebClient(); client.Headers.Add("PddToken", token); client.Encoding = Encoding.UTF8; string result = client.DownloadString(getMail); JObject json = JObject.Parse(result); var accounts = from account in json["accounts"] select account["fname"].Value<string>(); return accounts.ToList(); } 

I send a list of last names, but I want to structure it and transfer the received data to such a model, for example

 public class Mail { public string Fname { get; set; } } 

And then display already in View.

  • one
    what is the problem? Create an object of the required class in the select and that's it - Grundy
  • Can you give an example? - shatoidil

2 answers 2

The constructor can be called directly in the select.

 var accounts = from account in json["accounts"] select new Mail(){ Fname = account["fname"].Value<string>() }; 
     var list = new List<Mail>(); foreach(var account in accounts) { list.Add(new Mail() { Fname = account; }); } 
    • one
      better immediately in select, which is already in question - Grundy
    • one
      If a person asks such a question - then he needs to answer as clearly as possible IMHO. And in general, most of this design can be replaced by a concise LINQ-lambda. - gromanev
    • one
      since it already uses lambda, it’s enough just to call a constructor in a select. And so without explanation he will sculpt on three foreach - Grundy
    • @Grundy is not lambda but LINQ for that matter. Code in a short record! = Clear code, especially for a beginner, I think that it is not necessary to argue! - gromanev
    • Actually, your code does not help much, and there is no difference between your code without explanation, or the code from the question - Grundy