Linq query JObject

后端 未结 4 1144
日久生厌
日久生厌 2021-01-19 01:15

I am using Json.net for serializing and then making an JObject that looks like this:

 \"RegistrationList\": [
    {
      \"CaseNumber\": \"120654-1330\",
           


        
4条回答
  •  情书的邮戳
    2021-01-19 02:06

    I only want to display the CaseNumber, FirstName and Comment.

    As always in ASP.NET MVC you could start by writing a view model that matches your requirements:

    public class MyViewModel
    {
        public string CaseNumber { get; set; }
        public string FirstName { get; set; }
        public string Comment { get; set; }
    }
    

    then in your controller action you build the view model from the JObject instance you already have:

    public ActionResult Index()
    {
        JObject json = ... the JSON shown in your question (after fixing the errors because what is shown in your question is invalid JSON)
    
        IEnumerable model =
            from item in (JArray)json["RegistrationList"]
            select new MyViewModel
            {
                CaseNumber = item["CaseNumber"].Value(),
                FirstName = item["Person"]["FirstName"].Value(),
                Comment = item["User"]["Comment"].Value(),
            };
    
        return View(model);
    }
    

    and finally in your strongly typed view you display the desired information:

    @model IEnumerable
    
    
            @foreach (var item in Model)
            {
                
            }
        
    Case number First name Comment
    @item.CaseNumber @item.FirstName @item.Comment

提交回复
热议问题