c# - How to get name data of multiple json object list that will be posted to web api? -


i have post method in web api asp.net accepts single json object. here code code check if data posting json existing in database. , using linq interact db. sales_ auto-generated in context, believe entityframework. db database object.

public httpresponsemessage postsales(sales sales, [fromuri] string auth)         {             try             {                 if (modelstate.isvalid)                 {                                         if (auth == "kdi")                     {                          int64 rs = db.sales_.where(sl => sl.serial == sales.serial).count();                         if (rs == 1)                         {                             return request.createerrorresponse(httpstatuscode.conflict, " duplicate found!");                         }                         else                         {                             db.sales_.add(sales);                             db.savechanges();                             return request.createerrorresponse(httpstatuscode.ok, "added!");                         } //closing elses cut out question. 

that working fine , accepts json fine single object.

here json posted.

{   "serial": 1,   "extserial": "ah0000002",   "date": "2015-03-01",   "customerrefnbr": "jpm0001",   "description": "2015 february rental  2015 february rental",   "customer": "trde0065",   "amount": 17989.51,   "aq_branch": "kdi",   "aq_coa": "4100503000",   "linesubaccount": "opmopn000",   "linetaxcategory": "jpmtax",   "linequantity": 1,   "lineunitprice": 400000,   "aq_poststatus": 1,   "aq_statusdate": "2015-03-01",   "dts": "2015-03-01",   "linedescription": "line description" } 

now, wanted post multiple lists or lines (not sure call them) in json object. this:

    {       "extserial": "ah0000002",       "date": "2015-03-01",       "customerrefnbr": "jpm0001",       "description": "2015 february rental  2015 february rental",       "customer": "trde0065",       "amount": 17989.51,       "aq_branch": "kdi",       "aq_coa": "4100503000",       "linesubaccount": "opmopn000",       "linetaxcategory": "jpmtax",       "linequantity": 1,       "lineunitprice": 400000,       "aq_poststatus": 1,       "aq_statusdate": "2015-03-01",       "dts": "2015-03-01",       "linedescription": "line description"     },{       "extserial": "ah0000003",       "date": "2015-04-01",       "customerrefnbr": "jpm0002",       "description": "2015 february rental  2015 february rental",       "customer": "trde0066",       " amount": 17989.51,       "aq_branch": "kdi",       "aq_coa": "4100503000",       "linesubaccount": "opmopn000",       "linetaxcategory": "jpmtax",       "linequantity": 2,       "lineunitprice": 400000,       "aq_poststatus": 1,       "aq_statusdate": "2015-04-01",       "dts": "2015-04-01",       "linedescription": "line description" } 

not sure how read on post method though. have tried changing parameter sales list<sales> sales. think use list in object problem db.sales_.add(sales); producing error now. (invalid arguments) not sure how use .add() list. also, can't data of sales table column serial shown in int64 rs = db.sales_.where(sl => sl.serial == sales.serial).count();

i'm pretty stuck , don't know how this. hope don't find question stupid. need help. know it's simple fix.

json arrays wrapped in [], sales argument must of list or array.

change method signature to

public httpresponsemessage postsales(list<sales> sales, [fromuri] string auth) 

change json wrapped in [] like

[{       "extserial": "ah0000002",       "date": "2015-03-01",       "customerrefnbr": "jpm0001",       "description": "2015 february rental  2015 february rental",       "customer": "trde0065",       "amount": 17989.51,       "aq_branch": "kdi",       "aq_coa": "4100503000",       "linesubaccount": "opmopn000",       "linetaxcategory": "jpmtax",       "linequantity": 1,       "lineunitprice": 400000,       "aq_poststatus": 1,       "aq_statusdate": "2015-03-01",       "dts": "2015-03-01",       "linedescription": "line description"     },{       "extserial": "ah0000003",       "date": "2015-04-01",       "customerrefnbr": "jpm0002",       "description": "2015 february rental  2015 february rental",       "customer": "trde0066",       " amount": 17989.51,       "aq_branch": "kdi",       "aq_coa": "4100503000",       "linesubaccount": "opmopn000",       "linetaxcategory": "jpmtax",       "linequantity": 2,       "lineunitprice": 400000,       "aq_poststatus": 1,       "aq_statusdate": "2015-04-01",       "dts": "2015-04-01",       "linedescription": "line description" }] 

now enumerate through sales , process each 1 individually. like

parallel.foreach(sales, sale => { //you can clean using any(), instead of where..  //int64 rs = db.sales_.where(sl => sl.serial == sale.serial).count();     using (var dbcontext = new yourdatabasecontext())     {                         if (dbcontext .sales_.any(sl => sl.serial == sale.serial))                         {                             return;  //ignore duplicates                         }                         else                         {                             dbcontext .sales_.add(sale);                             dbcontext .savechanges();                         }     } }); return request.createerrorresponse(httpstatuscode.ok, "finished"); //you can return ok considering duplicate not break client. 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -