locked
adding 2 different generic list into one c#.net RRS feed

  • Question

  • User1804579801 posted

    my requriement  I should get below response from my webservice to access in mobile app 

    {

        "status": 0,

        "message": "sucess",

        "data": [

            {

                "name": "name",

                "batchname": "batch",

                "vState": "Telangana",

                "vDistrict": "Hyderabad"

            }

        ],

        "days": 1,

        "list": [

            {

                "date": "2019-11-21",

                "in": "In",

                "test": "Pre-Test",

                "fee": "",

                "status": null

            },

            {

                "date": "2019-11-22",

                "in": "In",

                "test": "",

                "fee": "",

                "status": null

            },

            {

                "date": "2019-11-23",

                "in": "In",

                "test": "",

                "fee": "",

                "status": null

            },

            {

                "date": "2019-11-24",

                "in": "In",

                "test": "",

                "fee": "",

                "status": null

            },

            {

                "date": "2019-11-25",

                "in": "In",

                "test": "Post-Test",

                "fee": "Feedback",

                "status": null

            }

        ]

    }

    I have 2 classes Response and Data  and added 2 new classes for list and day  as BatchList and Batchday

    my code in webserive

     Response res = new Response();
                     res.status = 0;
                     res.message = "success";
                     //res.auth_key = rdr["auth_key"].ToString();
                     Data batch = new Data();
                     {
                         batch.name= rdr["name"].ToString());
                         batch.batchname= rdr["batchname"].ToString();
    batch.vstate= rdr["state"].ToString();
    batch.vcity= rdr["city"].ToString(); }; dt = getbatchdetails(rdr["vUniqueCode"].ToString());//Get the Batch dates and NoofDays BatchDays day1 = new BatchDays(); { day1.nodays =int.Parse(dt.Rows[0]["NoofDays"].ToString()); res.days = day1; } List<BatchList> list_batchlist = new List<BatchList>(); for (int i = 0; i < dt.DefaultView.Count; i++) { BatchList list = new BatchList(); { list.date = dt.Rows[i]["date"].ToString(); list.In = "In"; if (i == 0) { list.test = "Pre-Test"; } else if (i == dt.DefaultView.Count - 1) { list.test = "Post-Test"; } else { list.test = ""; } list.fee = ""; list.status = "null"; list_batchlist.Add(list); } } list_usertraining.Add(res); this.Context.Response.ContentType = "application/json; charset=utf-8"; JavaScriptSerializer js = new JavaScriptSerializer() { MaxJsonLength = 867530900 }; //string JSONResult = js.Serialize(list_usertraining); string JSONResult = js.Serialize(list_usertraining); string FinalJsonstring1 = JSONResult.Replace("[", ""); string FinalJsonstring2 = FinalJsonstring1.Replace("]", ""); Context.Response.Write(FinalJsonstring2);

    I should return a json string from web service to mobile app .I tried below code 

    // put one into a dict for faster access
    var dict2 = list_batchlist.ToDictionary(x => x.date);
    // merge the lists using ??
    var merged = list_usertraining.Select(x => 
    {
        var p2 = dict2[x.date];
        return new Response()
        {
            date = x.date,
            In = x.In ?? p2.In,
            test = x.test ?? p2.test,
            fee = x.fee,
            status = x.status
        };
    });
         

    but i should pass to   string JSONResult = js.Serialize() .Please suggest 

    Friday, November 22, 2019 12:45 PM

All replies

  • User288213138 posted

    Hi nagapavanich.

    nagapavanich

    my requriement  I should get below response from my webservice to access in mobile app 

    You can try to put all the classes into one class. This is my test code:

    class Data
            {
                public int status { get; set; }
                public string message { get; set; }
                public List<Product> data { get; set; }
                public int days { get; set; }
    
                public List<Product1> list { get; set; }
            }
            class Product
            {
                public string name { get; set; }
                public string batchname { get; set; }
                public string vState { get; set; }
                public string vDistrict { get; set; }
            }
            class Product1
            {
                public string date { get; set; }
                public string in1 { get; set; }
                public string test { get; set; }
                public string fee { get; set; }
                public string status { get; set; }
    
            }
            public void Test1()
            {
                Data d1 = new Data();
                d1.status = 0;
                d1.message = "success";
                d1.data = new List<Product> { new Product { name = "name", batchname = "batchname", vState = "Telangana", vDistrict = "Hyderabad" } };
                d1.days = 1;
                d1.list = new List<Product1> { new Product1 { date = "2019-11-21", in1 = "In", test = "Pre-Test", fee = "", status = null },new Product1 { date = "2019-11-22", in1 = "In", test = "", fee = "", status = null }, new Product1 { date = "2019-11-23", in1 = "In", test = "", fee = "", status = null }, new Product1 { date = "2019-11-24", in1 = "In", test = "Post-Test", fee = "Feedback", status = null } };
                JavaScriptSerializer js = new JavaScriptSerializer() { MaxJsonLength = 867530900 };
                string JSONResult = js.Serialize(d1);
                string FinalJsonstring1 = JSONResult.Replace("[", "");
                string FinalJsonstring2 = FinalJsonstring1.Replace("]", "");           
                Response.Write(FinalJsonstring2);
    
            }

    The result:

    Best regards,

    Sam

    Monday, November 25, 2019 8:17 AM
  • User1804579801 posted
     d1.list = new List<Product1> { new Product1 { date = "2019-11-21", in1 = "In", test = "Pre-Test", fee = "", status = null },new Product1 { date = "2019-11-22", in1 = "In", test = "", fee = "", status = null }, new Product1 { date = "2019-11-23", in1 = "In", test = "", fee = "", status = null }, new Product1 { date = "2019-11-24", in1 = "In", test = "Post-Test", fee = "Feedback", status = null } };

    the list you added is static   training may be multiple days or single day program how to achieve this  checked below code but did not work 

    for (int i = 0; i < dt.DefaultView.Count; i++)
                        {
                            if (i == 0)
                            {
                                d1.list = new List<DataDescDetails> { new DataDescDetails { date = dt.Rows[i]["date"].ToString(), In = "", test = "Pre-Test", fee = "", status = null } };
                            }
                            else if (i == dt.DefaultView.Count - 1)
                            {
                                d1.list = new List<DataDescDetails> { new DataDescDetails { date = dt.Rows[i]["date"].ToString(), In = "", test = "Post-Test", fee = "", status = null } };
                            }
                            else
                            {
                             d1.list = new List<DataDescDetails> { new DataDescDetails { date = dt.Rows[i]["date"].ToString(), In = "", test = "", fee = "", status = null } };
                            }
                        }

    It only adds the last column from the data table in the list 

    {
    "status": 0,
    "message": "success",
    "auth_key": null,
    "data": {
    "iTrainingId": 13,
    "vTrainingName": "178280",
    "dStartdate": "25-11-2019 00:00:00",
    "dEnddate": "28-11-2019 00:00:00",
    "vState": "Telangana",
    "vDistrict": "Hyderabad",
    "eTrainingMode": "Offline",
    "vUniqueCode": "13"
    },
    "days": 3,
    "list": {
    "date": "2019-11-28",
    "In": "",
    "test": "Post-Test",
    "fee": "",
    "status": null
    }
    }

    Always displaying the last date if 4 days program .check the above result. Please suggest 

    Monday, November 25, 2019 9:52 AM
  • User288213138 posted

    Hi nagapavanich,

    the list you added is static   training may be multiple days or single day program how to achieve this  checked below code but did not work

    You can try to use list<T>.add method to add the data into list.

    This is my test code:

     public void Test1()
            {
                Data d1 = new Data();
                d1.status = 0;
                d1.message = "success";
                d1.data = new List<Product> { new Product { name = "name", batchname = "batchname", vState = "Telangana", vDistrict = "Hyderabad" } };
                d1.days = 1;
                d1.list = new List<Product1>();
                for (int i = 0; i < 4; i++)
                {
                    if (i == 0)
                    {
                        d1.list.Add(new Product1 { date = "2019-11-21", in1 = "", test = "Pre-Test", fee = "", status = null });
                    }
                    else if (i == 4 - 1)
                    {
                        d1.list.Add(new Product1 { date = "2019-11-22", in1 = "", test = "Pre-Test", fee = "", status = null });
                    }
                    else
                    {            
                        d1.list.Add(new Product1 { date = "2019-11-23", in1 = "", test = "", fee = "", status = null });
                    }
                }
    
                JavaScriptSerializer js = new JavaScriptSerializer() { MaxJsonLength = 867530900 };
                string JSONResult = js.Serialize(d1);
                string FinalJsonstring1 = JSONResult.Replace("[", "");
                string FinalJsonstring2 = FinalJsonstring1.Replace("]", "");
                Response.Write(FinalJsonstring2);
    
            }

    Best regards,

    Sam

    Tuesday, November 26, 2019 5:56 AM