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[,]
C#
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 MohantyRank #112 · 20K reputationPosted May 4, 2023, 6:00 AM EDT
To skip the
ContractInvoicePerDaysIDwhen theifcondition fails, you can use thecontinuestatement to move to the next iteration of the loop:foreach (var Item in lstContractInvoicePerDays) { DateTime _LastModifiedDateTC = Global.GetTimecardLastModifiedDate(Item.ContractInvoicePerDayIDs, (int)InvoiceSourceTypes.PerDay).GetValueOrDefault(); if (Item.LastModifiedDate != _LastModifiedDateTC) { continue; // skip to next iteration if condition fails } contractInvoicePerDayIDs = string.Join(",", Item.ContractInvoicePerDayIDs); // rest of your code here }Tuhin PaulRank #7 · 71.9K reputationPosted May 4, 2023, 7:59 AM EDT
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:
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) { if (!string.IsNullOrEmpty(contractInvoicePerDayIDs)) { contractInvoicePerDayIDs += ","; } contractInvoicePerDayIDs += string.Join(",", Item.ContractInvoicePerDayIDs); } }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.