Hello There, Guest!
View New Posts  |  View Today's Posts
[VB.Net] LINQ GroupBy, Loop thru group on condition.

  • 0 Vote(s) - 0 Average


05-08-2012, 02:22 PM #1
_HAWK_
Interrogator(Of Foo)
***
Posts: 337 Threads:21 Joined: Jun 2011 Reputation: 10

LINQ GroupBy, Loop thru group on condition.
I have a dataTable where I can have several record with the name value in a certain column. I want to grab groups of the duplicate values records to send to a function that will evaluate each row to determine which record it can delete - if any. I am trying to use the GroupBy function, but not sure about the capturing the groups that have more than 1 record.

Code:
Dim list = dt.AsEnumerable.GroupBy(Function(dr) dr("Number"))

Ideas?
Despite all my rage I am still just a rat in a cage... {SP}

05-08-2012, 09:36 PM #2
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: LINQ GroupBy, Loop thru group on condition.
Can you provide me with what that data table looks like? Then I can help you. It's quite easy actually too

Edit; i'm also unsure on how you want to remove the records? Does it matter which record you leave behind if there's duplicates? I don't understand.

Also are you comparing the items in one column to other columns/rows? (Everything in the table?) The information here is a bit limited for me
This post was last modified: 05-08-2012, 09:42 PM by AceInfinity.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

05-08-2012, 09:44 PM #3
_HAWK_
Interrogator(Of Foo)
***
Posts: 337 Threads:21 Joined: Jun 2011 Reputation: 10

RE: LINQ GroupBy, Loop thru group on condition.
Simple layout, 20 columns, thousands of rows. I can give you more later.
Despite all my rage I am still just a rat in a cage... {SP}

05-09-2012, 02:52 AM #4
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: LINQ GroupBy, Loop thru group on condition.
But are you comparing each row for duplicates between columns? Or each item within the databable, where ever, to everything else in the datatable? I need to understand what you're comparing.

Here's an example of GroupBy that i've put together for you:
Code:
Dim a1 As New List(Of String) From {"one", "two", "three", "four", "five", _
                                    "1", "2", "3", "4", "5", _
                                    "one1", "two", "three3", "four4", "five5"}

Dim f As Func(Of String, Boolean) = Function(x) a1.FindAll(Function(i) x = i).Count > 1
Array.ForEach(a1.GroupBy(f)(1).ToArray, Sub(x) Console.WriteLine(x))

It's beautiful for a simple example :)

To understand how many were grouped, you would have to use the Count() method, i've joined a few things here, but the GroupBy clause--part, could be separated into a variable, then count that variable.
This post was last modified: 05-09-2012, 03:06 AM by AceInfinity.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

05-09-2012, 01:39 PM #5
_HAWK_
Interrogator(Of Foo)
***
Posts: 337 Threads:21 Joined: Jun 2011 Reputation: 10

RE: LINQ GroupBy, Loop thru group on condition.
Comparing same column in entire datatable for duplicates. When 2 or more are found send them to a sub for further processing - some will get deleted. Mainly I am building a new datatable based on all rows that are unique and all duplicates that did not get removed. I will play with the example above.

Thanks

Code:
Dim mylist = dt.AsEnumerable().ToList()
Dim f As Func(Of DataRow, Boolean) = Function(x) mylist.FindAll(Function(i) x("Number").ToString = i("Number").ToString).Count > 1
Array.ForEach(mylist.GroupBy(f)(1).ToArray, Sub(x) Debug.WriteLine(x("Number").ToString))

This gives me all the rows, not the duplicates.
This post was last modified: 05-09-2012, 01:57 PM by _HAWK_.
Despite all my rage I am still just a rat in a cage... {SP}

05-09-2012, 05:50 PM #6
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: LINQ GroupBy, Loop thru group on condition.
Quote:This gives me all the rows, not the duplicates.

You're not using it correctly then... Run my example by itself and see what it returns, play with that a bit more before experimenting with your actual table.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

05-09-2012, 06:56 PM #7
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: LINQ GroupBy, Loop thru group on condition.
I'll show you what's really happening here by trimming it down further:
Code:
Dim a1 As New List(Of String) From {"one", "two", "three", "four", "five", _
                                    "1", "2", "3", "4", "5", _
                                    "one1", "two", "three3", "four4", "five5", _
                                    "test", "test", "test"}

Dim f As Func(Of String, Boolean) = Function(x) a1.FindAll(Function(i) x = i).Count > 1
Dim g As IEnumerable(Of IGrouping(Of Boolean, String)) = a1.GroupBy(f)
Array.ForEach(g(2).ToArray, Sub(x) Console.WriteLine(x))

I like to work with actual data though when I help people, so if there's a way you can help me duplicate your DataTable then I can have something to work with to provide you with an example I know suits you're situation. Sometimes my help isn't the best, because I don't fully understand what OP is trying to do, only what they ask to do, and if they give me more info I can sometimes come out with even better solutions.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

05-09-2012, 07:04 PM #8
_HAWK_
Interrogator(Of Foo)
***
Posts: 337 Threads:21 Joined: Jun 2011 Reputation: 10

RE: LINQ GroupBy, Loop thru group on condition.
I ran your example and it returns "two" twice. That is why I changed it like above - to return similar datarows. I don't understand why it return all of them. Do you see an error in my code?
Despite all my rage I am still just a rat in a cage... {SP}

05-09-2012, 07:12 PM #9
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RE: LINQ GroupBy, Loop thru group on condition.
Quote:I ran your example and it returns "two" twice.

Yeah, :) I hope that's what you want anyways lol. See, what my code does is it groups the elements of the array where it's found more than one of the same element.

I haven't looked to far at your code, I was in a rush to get to work this morning. I'm literally alternating jobs all week this week, even Friday now. Back to one job one day, to another job the next day, back to the previous job the next day...


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

05-09-2012, 07:17 PM #10
_HAWK_
Interrogator(Of Foo)
***
Posts: 337 Threads:21 Joined: Jun 2011 Reputation: 10

RE: LINQ GroupBy, Loop thru group on condition.
Ok, I just checked the 0 index of that group and it came up with 86 dups in 1475, the number I was getting was the 1389 - which at first glance without using the .Count on the array it appeared to be the entire list.

Code:
Debug.WriteLine(mylist.GroupBy(f)(0).ToArray.Count.ToString)


I just have to check them to see if they are correct/duplicates.

Index 0 shows all the dups and index 1 shows all the unique rows - sweet! Done! You have no idea how helpful that was!!!
This post was last modified: 05-09-2012, 07:38 PM by _HAWK_.
Despite all my rage I am still just a rat in a cage... {SP}




Forum Jump:


Possibly Related Threads...
Thread Author Replies Views Last Post
   A better way to loop? Lee Stevens 3 2,183 03-09-2014, 05:44 PM
Last Post: KoBE
Exclamation  Find max in do while loop vb.net polas5 9 6,216 02-15-2014, 09:45 AM
Last Post: polas5
   LINQ DataTable Lee Stevens 2 2,100 12-23-2013, 08:05 PM
Last Post: Lee Stevens
   Switching Array Values - LINQ AceInfinity 5 3,366 12-14-2013, 05:31 PM
Last Post: AceInfinity
   How do you make a do until loop output from highest to lowest? william7 3 2,175 03-01-2013, 12:11 AM
Last Post: AceInfinity


Users browsing this thread: 1 Guest(s)