dt1-
expected output -
using below VB.net code -
Dim resultTableB As New DataTable
resultTableB.Columns.Add(“PRFNR”, GetType(String))
resultTableB.Columns.Add(“EMPNR”, GetType(String))
resultTableB.Columns.Add(“BUDAT”, GetType(DateTime))
resultTableB.Columns.Add(“ATDCD”, GetType(String))
’ Configuration
Dim beforeLCCount As Integer = 2 ’ Number of consecutive “LC” codes before “P”
Dim afterLCCount As Integer = 2 ’ Number of consecutive “LC” codes after “P”
Dim dtModified As DataTable = dt1.Copy()
Dim resultRows As New List(Of DataRow)
For Each empnrGroup In dtModified.AsEnumerable().GroupBy(Function(r) r.Field(Of String)(“EMPNR”))
Dim empGroup = empnrGroup.ToList()
For Each row In empGroup
If row.Field(Of String)("ATDCD") = "P" Then
Dim beforeP = New List(Of DataRow)()
Dim afterP = New List(Of DataRow)()
Dim includeCurrentP = False
' Check for consecutive "LC" codes before "P"
Dim startIndexBeforeP As Integer = empGroup.IndexOf(row) - beforeLCCount
If startIndexBeforeP >= 0 AndAlso empGroup.GetRange(startIndexBeforeP, beforeLCCount).All(Function(r) r.Field(Of String)("ATDCD") = "LC") Then
beforeP.AddRange(empGroup.GetRange(startIndexBeforeP, beforeLCCount))
includeCurrentP = True
End If
' Check for consecutive "LC" codes after "P", allowing "R" immediately after "P"
Dim indexAfterP As Integer = empGroup.IndexOf(row) + 1
If indexAfterP < empGroup.Count AndAlso empGroup(indexAfterP).Field(Of String)("ATDCD") = "R" Then
afterP.Add(empGroup(indexAfterP))
indexAfterP += 1 ' Skip "R" for the next check
End If
Dim endIndexAfterP As Integer = indexAfterP + afterLCCount - 1
If endIndexAfterP < empGroup.Count AndAlso empGroup.GetRange(indexAfterP, afterLCCount).All(Function(r) r.Field(Of String)("ATDCD") = "LC") Then
afterP.AddRange(empGroup.GetRange(indexAfterP, afterLCCount))
includeCurrentP = True
End If
' If both conditions are met, add the rows to resultRows
If includeCurrentP Then
resultRows.AddRange(beforeP)
resultRows.Add(row)
resultRows.AddRange(afterP)
End If
End If
Next
Next
’ Distinct and Import
Dim distinctRows = resultRows.GroupBy(Function(r) r).Select(Function(g) g.First()).ToList()
For Each row In distinctRows
resultTableB.ImportRow(row)
Next
result = resultTableB
but below line items are missing
ESTATE BATURONG 1 | 1LMS/IOI/0123/40764 | 2/13/2024 | LC |
---|---|---|---|
ESTATE BATURONG 1 | 1LMS/IOI/0123/40764 | 2/14/2024 | LC |
ESTATE BATURONG 1 | 1LMS/IOI/0123/40764 | 2/14/2024 | R |
ESTATE BATURONG 1 | 1LMS/IOI/0123/40764 | 2/16/2024 | P |
please help me to reslove