Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3485018
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:51:24+00:00 2026-05-18T10:51:24+00:00

I have a DataTable which I want to check if values in three of

  • 0

I have a DataTable which I want to check if values in three of the columns are unique. If not, the last column should be filled with the line number of the first appearance of the value-combination.

For example, this table:

ID    Name    LastName    Age    Flag
-------------------------------------
1     Bart    Simpson     10      -
2     Lisa    Simpson      8      -
3     Bart    Simpson     10      -
4     Ned     Flanders    40      -
5     Bart    Simpson     10      -

Should lead to this result:

Line  Name    LastName    Age    Flag
-------------------------------------
1     Bart    Simpson     10      -
2     Lisa    Simpson      8      -
3     Bart    Simpson     10      1
4     Ned     Flanders    40      -
5     Bart    Simpson     10      1

I solved this by iterating the DataTable with two nested for loops and comparing the values. While this works fine for a small amount of data, it gets pretty slow when the DataTable contains a lot of rows.

My question is: What is the best/fastest solution for this problem, regarding that the amount of data can vary between let’s say 100 and 20000 rows?
Is there a way to do this using LINQ? (I’m not too familiar with it, but I want to learn!)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:51:25+00:00Added an answer on May 18, 2026 at 10:51 am

    Okay, I think I got an answer myself. Based on the suggestion in James Wiseman’s answer, I tried something with LINQ.

    Dim myErrnrFnct = Function( current, first) If(first <> current, first, 0)
    Dim myQuery = From row As DataRow In myDt.AsEnumerable _
                          Select New With { _
                            .LINE = row.Item("LINE"), _
                            .NAME = row.Item("NAME"), _
                            .LASTNAME = row.Item("LASTNAME"), _
                            .AGE = row.Item("AGE"), _
                            .FLAG = myErrnrFnct(row.Item("LINE"), myDt.AsEnumerable.First(Function(rowToCheck) _
                                                                                            rowToCheck.Item("NAME") = row.Item("NAME") AndAlso _
                                                                                            rowToCheck.Item("LASTNAME") = row.Item("LASTNAME") AndAlso _
                                                                                            rowToCheck.Item("AGE") = row.Item("AGE")).Item("LINE")) _
                          }
    

    With this query I get exactly the result that’s described in the Question. The myErrnrFnct Function is necessary because I want the Flag column to have the value 0 if there is no other row with the same values.

    To get a DataTable out of myQuery again, I had to add some extensions described here:
    How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow
    And then, this line will do:

    Dim myNewDt As DataTable = myQuery.CopyToDataTable()
    

    This seems to work just fine. Any suggestions to do this better?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.