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 :
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();
}
}
}
Tuhin PaulPosted Apr 28, 2023, 2:31 AM
Complete code with the solution:
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.
Rajkiran SwainPosted Apr 27, 2023, 11:54 AM