I have a input datatable as below. I want to check and remove the row if they have same value at “item” Col and the value at “status” Col equals “OK”
I have made linq code as below but it run not correctly.
Is there any wrong in my code? and how to fix it?
Thanks in advance!
(From row In Table.AsEnumerable()
Group row By k1= row(“item”).toString.Trim,k2= row(“status”).toString.Trim.Where(Function(g) g.ToString.Contains(“OK”)) Into grp=Group
Let r = grp.Last
Order By Array.IndexOf(Table.AsEnumerable.toArray,r)
Select r).CopyToDatatable()
(From d in dtData.AsEnumerable
Group d By k1= row(“item”).toString.Trim into grp=Group
let s = grp.Sum(Function (x) CInt(x("quantitiy").toString.Trim))
Let r = grp.Last()
Order By Array.IndexOf(dtData.AsEnumerable.toArray,r)
let ra = new Object(){k1, s, r("Location"),r("status") }
Select dtResult.Rows.Add(ra)).CopyToDataTable
For output 1 following is imprtant but wa not answered
Give a try on following:
(From d in dtData.AsEnumerable
Group d By k1= row(“item”).toString.Trim into grp=Group
Let r = grp.Last()
Order By Array.IndexOf(dtData.AsEnumerable.toArray,r)
Select r).CopyToDataTable
Ok will have a look on it. Clear requirements help always for better solution. Thats was the reason, why questions were asked above.
The requirement looks different can you please confirm?
Group data by item column #C1 - If group members count = 1 - take member #C2 - If group members count > 1 - filter out all members where status =/contains “OK”
out1 - take last remaining group member
Whats to do if #C2 Count = 0?
out2 - sum up #C1 / #C2 - other values take from last group member
(From d in dtData.AsEnumerable
Group d By k1= row(“item”).toString.Trim into grp=Group
Let chk = grp.Any(Function (x) x("status").toString.Contains("OK"))
Select chk).Any(Function (x) x)
returns true if there is 1 or more groups (key=item) where at least 1 member status contains ok