How to remove only one row among duplicate rows in a datatable - vb.net

I've a datatable dtPackageTest with following rows in it
testid testname
------ -----------
1 abc
2 xyz
1 abc
2 xyz
I followed this answer to but it removes all the duplicate rows, and my expected output is
testid testname
------ -----------
1 abc
2 xyz
My code:
Dim tblDups = From r In dtPackageTest _
Group By Dups = New With {Key .testid = CInt(r("testid")), Key .test = CStr(r("test"))} Into Group _
Where (Group.Count > 1) _
Select Dups
Dim dupRowList = (From r In dtPackageTest _
Join dupRow In tblDups _
On dupRow.testid Equals CInt(r("testid")) _
And dupRow.test Equals CStr(r("test")) _
Select r).ToList()
For Each dup In dupRowList
dtPackageTest.Rows.Remove(dup)
Next

Make following changes in your existing code,this will work as you expected :
(I guess this should be an old school logic but it works )
'Add order by - Order By Dups.testid
Dim tblDups = From r In dtPackageTest _
Group By Dups = New With {Key .testid = CInt(r("testid")), Key .test = CStr(r("test"))} Into Group _
Where (Group.Count > 1) Order By Dups.testid _
Select Dups
'Add order by - Order By r("testid")
Dim dupRowList = (From r In dtPackageTest _
Join dupRow In tblDups _
On dupRow.testid Equals CInt(r("testid")) _
And dupRow.test Equals CStr(r("test")) Order By r("testid") _
Select r).ToList()
Dim id As Integer = 0
For Each dup In dupRowList
'Checking for testid is already removed or not
If id <> dup("testid") Then
id = dup("testid")
dtPackageTest.Rows.Remove(dup)
End If
Next

Related

datatable sum column and concatenate rows using LINQ and group by on multiple columns

I Have a datatable with following records
ID NAME VALUE CONTENT
1 AAA 10 SYS, LKE
2 BBB 20 NOM
1 AAA 15 BST
3 CCC 30 DSR
2 BBB 05 EFG
I want to write a VB.NET/LINQ query to have a output like below table: -
ID NAME SUM CONTENT (as CSV)
1 AAA 25 SYS, LKE, BST
2 BBB 25 NOM, EFG
3 CCC 30 DSR
Please provide me LINQ query to get the desired result. Thanks.
I have tried concatenation using below query
Dim grouped = From row In dtTgt.AsEnumerable() _
Group row By New With {row.Field(Of Int16)("ID"), row.Field(Of String)("Name")} _
Into grp() _
Select ID, Name, CONTENT= String.Join(",", From i In grp Select i.Field(Of String)("CONTENT"))
This query will give you the expected output:-
Dim result = From row In dt.AsEnumerable()
Group row By _group = New With {Key .Id = row.Field(Of Integer)("Id"),
Key .Name = row.Field(Of String)("Name")} Into g = Group
Select New With {Key .Id = _group.Id, Key .Name = _group.Name,
Key .Sum = g.Sum(Function(x) x.Field(Of Integer)("Value")),
Key .Content = String.Join(",", g.Select(Function(x) x.Field(Of String)("Content")))}
Thanks for your answers.
However, I have managed to get the desired result using simple code (Without LINQ): -
Dim dt2 As New DataTable
dt2 = dt.Clone()
For Each dRow As DataRow In dt.Rows
Dim iID As Integer = dRow("ID")
Dim sName As String = dRow("Name")
Dim sContt As String = dRow("Content")
Dim iValue As Integer = dRow("Value")
Dim rwTgt() As DataRow = dt2.Select("ID=" & iID)
If rwTgt.Length > 0 Then
rwTgt(0)("Value") += iValue
rwTgt(0)("Content") += ", " & sContt
Else
rw = dt2.NewRow()
rw("ID") = iID
rw("Name") = sName
rw("Value") = iValue
rw("Content") = sContt
dt2.Rows.Add(rw)
End If
Next

Using Linq for select row from DataTableA where id not in DataTableB

I have two dataTables ,and i want select all rows from DataTable1 where id is not in DataTable2.below what i have tried :
Sql = "select *,N°Reçu as NumRecu from V_Sit_J_Vente,V_Bien where V_Sit_J_Vente.Code_bien=V_Bien.Code_bien and date_situation <= '" + dt2 + "' and date_situation >= '" + dt1 + "'"
Dim GlobalDataVente As DataTable = utilitaire.getDataSet(Sql).Tables(0)
Sql = "select * from V_Reserv_Annule"
Dim GlobalDataAnnule As DataTable = utilitaire.getDataSet(Sql).Tables(0)
Dim query = (From order In GlobalDataVente.AsEnumerable() _
Where order!code_projet = tab.Rows(i).Item("code_projet")).ToList
Dim bannedCCList = From c In GlobalDataAnnule.AsEnumerable() _
Where c!type.Equals("Transfert acompte") = False And c!date_annule <= dt2
Dim exceptBanned = From c In query Group Join b In bannedCCList On c.Field(Of String)("N°Reçu") Equals b.Field(Of String)("num_reserv_remplace")
Into j() From x In j.DefaultIfEmpty() Where x Is Nothing Select c
What i want that "exceptBanned " containt all rows of "query" except row exist in "bannedCCList "
Thanks in advance
You can use Contains for this:
Dim query = (From order In GlobalDataVente.AsEnumerable() _
Where order!code_projet = tab.Rows(i).Item("code_projet")).ToList
Dim bannedCCList = From c In GlobalDataAnnule.AsEnumerable() _
Where c.type.Equals("Transfert acompte") = False And c.date_annule <= dt2
Select c.Field(Of String)("num_reserv_remplace")
Dim exceptBanned = From c In query
Where Not bannedCCList.Contains(c.Field(Of String)("N°Reçu"))
Select c
bannedCCList defines a query that produces the Id values you want to exclude; exceptBanned combines query with this list of Ids into a query that only runs once to return the final results. It works this way because bannedCCList is an IEnumerable. It isn't executed when it's defined, only when it's actually used.

PredicateBuilder + Join + VB.NET

I have a db that contains 3 tables
Products Table Suppliers Table Categories Table
Id Id Id
ProductName SupplierName CategoryName
Quantity . .
SupplierId . .
CategoryId
.
.
I am using PredicateBuilder to select Product accoring to the selected product field (Quantity,Productname...)
How Can I use PredicateBuilder or any other method to Select the Product according to Its Suppliername or Categoryname
Please I am using VB.NET I saw many C# examples but I can not understand it nor translate it
I am thinking of using join in predicateBuilder but I do not how !!!!
To be more clear What I want is to combine multiple field in one search ,Like for example:
Give me a Product where It's Name contains "s" and Quantity <10 and SupplierName is Kimo
Give me a product where i's name contains "g" only
give me the products for categoryName "Machines"
.
.
And this search predicate is changable because Each Products table has many fields ,So the search is dynamic according to selected fields
Waiting for your kind help.
I think Arion answer was correct but It needs some revision
AnyWay I came up with this solution ,It is not the most effecient one but It solved my problem.
Dim SupplierAlso As String = ""
Dim CategoryAlso As String = ""
Dim pred = PredicateBuilder.True(Of Products)()
Select Case Entry
Case "Number"
Dim Inum = Entry
pred = pred.And(Function(m As products) m.ID.Equals(CInt(Inum)))
Case "ProductName"
Dim IName = Entry
pred = pred.And(Function(m As Products) m.ProductName.IndexOf(IName, StringComparison.OrdinalIgnoreCase) >= 0)
.
.
.
Case "Supplier"
SupplierAlso = Entry
Case "Category"
CategoryAlso = Ent
Next
Dim f As ProductsDataTable = Products.Product
Dim tmp As IEnumerable(Of Products) = New ProductsDataTable().AsEnumerable()
tmp = f.AsEnumerable.Where(pred.Compile)
Dim qry
If CategoryAlso = "" And SupplierAlso = "" Then
q = (From prods In tmp
Join Cats In Categories
On prods.CategoryId Equals Cats.ID
Join Supps In Suppliers
On Supps.ID Equals prods.SupplierId
Select Supps.SupplierName, Cats.CategoryName, prods.ID _
, prods.ProductName, prods.UnitPrice, prods.CategoryId _
, prods.SupplierId, prods.Location, _
prods.Description, prods.SellPrice, prods.CountInStock _
, prods.ProductionDate, prods.ExpiryDate, _
prods.ProductType, prods.ProductSeason).ToList
ElseIf CategoryAlso <> "" And SupplierAlso <> "" Then
q = (From prods In tmp
Join Cats In Categories
On prods.CategoryId Equals Cats.ID
Join Supps In Suppliers
On Supps.ID Equals prods.SupplierId
Where Cats.CategoryName.IndexOf((CategoryAlso)
, StringComparison.OrdinalIgnoreCase) >= 0 And _ Supps.SupplierName.IndexOf((SupplierAlso), _
StringComparison.OrdinalIgnoreCase) >= 0
Select Supps.SupplierName, Cats.CategoryName, prods.ID, _
prods.ProductName, prods.UnitPrice, prods.CategoryId, _
prods.SupplierId, prods.Location, _
prods.Description, prods.SellPrice, prods.CountInStock, _
prods.ProductionDate, prods.ExpiryDate, _
prods.ProductType, prods.ProductSeason).ToList
ElseIf SupplierAlso <> "" And CategoryAlso = "" Then
q = (From prods In tmp
Join Cats In Categories
On Cats.ID Equals prods.CategoryId
Join Supps In Suppliers
On prods.SupplierId Equals Supps.ID Where _
Supps.SupplierName.IndexOf((SupplierAlso), _
StringComparison.OrdinalIgnoreCase) >= 0
Select Cats.CategoryName, Supps.SupplierName, prods.ID, _
prods.ProductName, prods.UnitPrice, prods.CategoryId, _
prods.SupplierId, prods.Location, _
prods.Description, prods.SellPrice, prods.CountInStock, _
prods.ProductionDate, prods.ExpiryDate, _
prods.ProductType, prods.ProductSeason).ToList
ElseIf CategoryAlso <> "" And SupplierAlso = "" Then
q = (From prods In tmp
Join Cats In Categories
On prods.CategoryId Equals Cats.ID Where Cats.CategoryName.IndexOf _
((CategoryAlso), StringComparison.OrdinalIgnoreCase) >= 0
Join Supps In Suppliers On Supps.ID Equals prods.SupplierId
Select Supps.SupplierName, Cats.CategoryName, prods.ID, _
prods.ProductName, prods.UnitPrice, prods.CategoryId, _
prods.SupplierId, prods.Location, _
prods.Description, prods.SellPrice, prods.CountInStock, _
prods.ProductionDate, prods.ExpiryDate, _
prods.ProductType, prods.ProductSeason).ToList
End If
For Each it In q
With it
DataGridView2.Rows.Add _
({.ID, .ProductName, .UnitPrice, .categoryname, .suppliername, .Location, _
.Description, .SellPrice, .CountInStock, _ .ProductionDate, .ExpiryDate, _
.ProductType, .ProductSeason})
End With
Next
So what do you think ,Is there a better way? Ofcourse yes?Silly question ,But Where?
So i would do something like this then. I have simplify the where statements but I think you will get my point:
dim checkCategoryName as boolean=true
dim checkSupplier as boolean=true
dim checkQuantity as boolean=true
dim query= db.Products.Select (function(p) p)
if checkCategoryName then
query=query _
.Where (function(p) db.Categories.Where (function(c) c.CategoryName="??" ) _
.Select (function(c) c.Id) _
.Contains(p.CategoryId))
end if
if checkSupplier then
query=query _
.Where (function(q) db.Suppliers.Where (function(s) s.SupplierName="??") _
.Select (function(s) s.Id) _
.Contains(q.SupplierId))
end if
if checkQuantity then
query=query.Where (function(q) q.Quantity<10)
end if
dim result=query.ToList()
Where db is the linq data context.

How to remove all duplicates in a data table in vb.net?

Consider my data table
ID Name
1 AAA
2 BBB
3 CCC
1 AAA
4 DDD
Final Output is
2 BBB
3 CCC
4 DDD
How can i remove the rows in the data table using Vb.Net
Any help is appreciated.
Following works if you only want the distinct rows(skip those with same ID and Name):
Dim distinctRows = From r In tbl
Group By Distinct = New With {Key .ID = CInt(r("ID")), Key .Name = CStr(r("Name"))} Into Group
Where Group.Count = 1
Select Distinct
' Create a new DataTable containing only the unique rows '
Dim tblDistinct = (From r In tbl
Join distinctRow In tblDistinct
On distinctRow.ID Equals CInt(r("ID")) _
And distinctRow.Name Equals CStr(r("Name"))
Select r).CopyToDataTable
If you want to remove the dups from the original table:
Dim tblDups = From r In tbl
Group By Dups = New With {Key .ID = CInt(r("ID")), Key .Name = CStr(r("Name"))} Into Group
Where Group.Count > 1
Select Dups
Dim dupRowList = (From r In tbl
Join dupRow In tblDups
On dupRow.ID Equals CInt(r("ID")) _
And dupRow.Name Equals CStr(r("Name"))
Select r).ToList()
For Each dup In dupRowList
tbl.Rows.Remove(dup)
Next
Here is your sample-data:
Dim tbl As New DataTable
tbl.Columns.Add(New DataColumn("ID", GetType(Int32)))
tbl.Columns.Add(New DataColumn("Name", GetType(String)))
Dim row = tbl.NewRow
row("ID") = 1
row("Name") = "AAA"
tbl.Rows.Add(row)
row = tbl.NewRow
row("ID") = 2
row("Name") = "BBB"
tbl.Rows.Add(row)
row = tbl.NewRow
row("ID") = 3
row("Name") = "CCC"
tbl.Rows.Add(row)
row = tbl.NewRow
row("ID") = 1
row("Name") = "AAA"
tbl.Rows.Add(row)
row = tbl.NewRow
row("ID") = 4
row("Name") = "DDD"
tbl.Rows.Add(row)
You can use the DefaultView.ToTable method of a DataTable to do the filtering like this:
Public Sub RemoveDuplicateRows(ByRef rDataTable As DataTable)
Dim pNewDataTable As DataTable
Dim pCurrentRowCopy As DataRow
Dim pColumnList As New List(Of String)
Dim pColumn As DataColumn
'Build column list
For Each pColumn In rDataTable.Columns
pColumnList.Add(pColumn.ColumnName)
Next
'Filter by all columns
pNewDataTable = rDataTable.DefaultView.ToTable(True, pColumnList.ToArray)
rDataTable = rDataTable.Clone
'Import rows into original table structure
For Each pCurrentRowCopy In pNewDataTable.Rows
rDataTable.ImportRow(pCurrentRowCopy)
Next
End Sub
Assuming you want to check all the columns, this should remove the duplicates from the DataTable (DT):
DT = DT.DefaultView.ToTable(True, Array.ConvertAll((From v In DT.Columns Select v.ColumnName).ToArray(), Function(x) x.ToString()))
Unless I overlooked it, this doesn't seem to be in the documentation (DataView.ToTable Method), but this also appears to do the same thing:
DT = DT.DefaultView.ToTable(True)

Problem VB.NET LINQ Group joining

Ok I know there is a lot of posts on this but I have trying to solve this for a few hours and my brain is completely off.
Here is the thing
I have a list of object with 3 properties (quantity, service, name)
I want to have all names and services in my list (kinda cross join) and the grouped quantity of the row corresponding. Not clear i guess
Quantity Service Name
3 Srv2 Bob
4 Srv2 Paul
2 Srv1 Paul
1 Srv2 Nick
I want as output
All the services and all the names and corresponding quantities (0 if none)
Srv1 Paul 2
Srv1 Bob 0
Srv1 Nick 0
Srv2 Paul 4
Srv2 Bob 3
Srv2 Nick 1
Here is what I got so far, but I dont even get the expected results
And I am acutally certain there is a pretty easy way of achieving what i want... I little help would be welcome...
Thanks
Dim services = (From a In interventions Select New With {.Service = a.Service}).Distinct()
Dim months = (From b In interventions Select New With {.Month = b.DateHeureIntervention.Month}).Distinct()
'Dim query = (From s In services _
' From m In months _
' From i In interventions.Where(Function(x) x.Service = s.Service And x.DateHeureIntervention.Month = m.Month).DefaultIfEmpty(New Intervention()) _
' Select New ChartItem With {.Service = s.Service, _
' .Name = MonthName(m.Month), _
' .Quantite = i.Quantite}).ToList()
'Return (From q In query _
' Group By srv = q.Service, n = q.Name Into Group _
' Select New ChartItem With {.Name = n, _
' .Service = srv, _
' .Quantite = Group.Sum(Function(i) i.Quantite)}).ToList()
Dim q = (From i In interventions _
Group Join s In services On s.Service Equals i.Service Into si = _
Group Join m In months On m.Month Equals i.DateHeureIntervention.Month _
From x In si.DefaultIfEmpty() _
Group By m = i.DateHeureIntervention.Month, srv = i.Service Into Group _
Select New ChartItem With {.Service = srv, _
.Name = MonthName(m), _
.Quantite = Group.Sum(Function(y) y.i.Quantite)}).ToList()
Return q
I got it here is my result, there surely is a better way, but anyhow, it works
Dim months As List(Of Integer) = (From b In interventions Select b.DateHeureIntervention.Month).Distinct().ToList()
Dim services As List(Of String) = (From c In interventions Select c.Service).Distinct().ToList()
Dim q = (From s In services _
From m In months _
Select New ChartItem With {.Name = MonthName(m), _
.Service = s, _
.Quantite = (From i In AccesDonneesHelper.GetInterventionsDetails() _
Where i.Service = s And i.DateHeureIntervention.Month = m _
Select i.Quantite).Sum()}).ToList()