here is my code where i need to skip the contractinvoiceperdaysid when if condition fails
and that contractinvoiceperdaysid whose if condition matches shall pass through by join condition using split[,]
List lstContractInvoicePerDays = new List();
lstContractInvoicePerDays = staffCIPDApprovedViewModel.lstContractInvoicePerDays.ToList();
string contractInvoicePerDayIDs = string.Empty;
foreach (var Item in lstContractInvoicePerDays)
{
//ContractInvoicePerDay contractInvoicePerDay = db.ContractInvoicePerDays.Find(Item.ContractInvoicePerDayID);
DateTime _LastModifiedDateTC = Global.GetTimecardLastModifiedDate(Item.ContractInvoicePerDayIDs, (int)InvoiceSourceTypes.PerDay).GetValueOrDefault();
if (Item.LastModifiedDate == _LastModifiedDateTC)
{
contractInvoicePerDayIDs = string.Join(",", Item.ContractInvoicePerDayIDs);
}
}
Amit MohantyPosted May 4, 2023, 10:00 AM
To skip the
ContractInvoicePerDaysIDwhen theifcondition fails, you can use thecontinuestatement to move to the next iteration of the loop:Tuhin PaulPosted May 4, 2023, 11:59 AM
A code that skips the `ContractInvoicePerDayID` when the `if` condition fails, and adds the `ContractInvoicePerDayID` to the `contractInvoicePerDayIDs` string only when the `if` condition is true:
The `if` condition checks if the `LastModifiedDate` property of the current `Item` object matches the `_LastModifiedDateTC` value. If the condition is true, the `ContractInvoicePerDayID` values are added to the `contractInvoicePerDayIDs` string using `string.Join`. If the condition is false, the `ContractInvoicePerDayID` values are skipped and the loop moves on to the next item in the list. I added a check for an existing value in `contractInvoicePerDayIDs` before adding a comma, so that the comma is only added when needed.