Here I have the requested Payload :
{
contractInvoicePerDayID : '1061, 1135',
invoiceObj : ['2023-04-24T04:37:45', '2023-04-24T04:37:45]
}
How can i declare it in my c#code as a List :
C#
int? BaseStateProvinceID = null;
List ContractInvoicePerDayIDs = staffCIPDApprovedViewModel.ContractInvoicePerDayIDs.Split(',').Select(int.Parse).ToList();
if (ContractInvoicePerDayIDs.Count == 1)
{
BaseStateProvinceID = staffCIPDApprovedViewModel.BaseStateProvinceID;
}
CIPDPushReqMod timeCardPushRequest = new CIPDPushReqMod();
using (SqlConnection connection = new SqlConnection(db.Database.Connection.ConnectionString))
{
try
{
using (SqlCommand command = new SqlCommand("spGetCIPDForPushToLawson", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@ContractInvoicePerDayIDs", SqlDbType.NVarChar).Value = staffCIPDApprovedViewModel.ContractInvoicePerDayIDs as object ?? DBNull.Value;
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
connection.Open();
using (var reader = command.ExecuteReader())
{
timeCardPushRequest.TimeCardLst = ((IObjectContextAdapter)db).ObjectContext.Translate(reader).ToList();
reader.NextResult();
timeCardPushRequest.AdditionalComponents = ((IObjectContextAdapter)db).ObjectContext.Translate(reader).ToList();
}
}
}
catch (Exception)
{
}
finally
{
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
}
}
Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 27, 2023, 7:54 AM EDT
List invoiceObj = new List { DateTime.Parse("2023-04-24T04:37:45"), DateTime.Parse("2023-04-24T04:37:45") };CIPDPushReqMod timeCardPushRequest = new CIPDPushReqMod(); timeCardPushRequest.TimeCardLst = invoiceObj.Select(x => new CIPDPushReqObj { InvoiceDate = x }).ToList();Tuhin PaulRank #7 · 71.9K reputationPosted Apr 27, 2023, 10:31 PM EDT
Complete code with the solution:
// Define the payload as a string string payload = @" { contractInvoicePerDayID : '1061, 1135', invoiceObj : ['2023-04-24T04:37:45', '2023-04-24T04:37:45'] }"; // Use Newtonsoft.Json to deserialize the payload into a dynamic object dynamic payloadObj = JsonConvert.DeserializeObject(payload); // Extract the contractInvoicePerDayID property and split it into a list of integers List contractInvoicePerDayIDs = ((string)payloadObj.contractInvoicePerDayID).Split(',').Select(int.Parse).ToList(); // Extract the invoiceObj property as a list of DateTime objects List invoiceObj = ((JArray)payloadObj.invoiceObj).Select(d => DateTime.Parse((string)d)).ToList(); // Use the extracted data in your code int? baseStateProvinceID = null; if (contractInvoicePerDayIDs.Count == 1) { baseStateProvinceID = staffCIPDApprovedViewModel.BaseStateProvinceID; } CIPDPushReqMod timeCardPushRequest = new CIPDPushReqMod(); using (SqlConnection connection = new SqlConnection(db.Database.Connection.ConnectionString)) { try { using (SqlCommand command = new SqlCommand("spGetCIPDForPushToLawson", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@ContractInvoicePerDayIDs", SqlDbType.NVarChar).Value = string.Join(",", contractInvoicePerDayIDs); if (connection.State != ConnectionState.Closed) { connection.Close(); } connection.Open(); using (var reader = command.ExecuteReader()) { timeCardPushRequest.TimeCardLst = ((IObjectContextAdapter)db).ObjectContext.Translate(reader).ToList(); reader.NextResult(); timeCardPushRequest.AdditionalComponents = ((IObjectContextAdapter)db).ObjectContext.Translate(reader).ToList(); } } } catch (Exception) { } finally { if (connection.State != ConnectionState.Closed) { connection.Close(); } } }In this code, first deserialize the payload into a dynamic object using Newtonsoft.Json. Then, extract the contractInvoicePerDayID property and split it into a list of integers using LINQ's `Select` and `ToList` methods. Next, extract the invoiceObj property as a list of DateTime objects using a combination of LINQ's `Select` and `ToList` methods, as well as DateTime.Parse. I used the extracted data in your code as before, but with one modification: join the `contractInvoicePerDayIDs` list into a comma-separated string and pass it as a parameter value to the SqlCommand.