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 406919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:32:37+00:00 2026-05-12T17:32:37+00:00

I have a very interesting LINQ question. I have a document, that I am

  • 0

I have a very interesting LINQ question. I have a document, that I am trying to filter results on, but to filter, I am matching on a REGEX result from one element of the XML.

I have the following, working LINQ to XML to get the individual data that I’m looking for.

Dim oDocument As XDocument
oDocument = XDocument.Load("test.xml")
Dim results = (From x In oDocument.Descendants.Elements("ROW") _
   Select New With {.ApplicationName = GetApplicationName(x.Element("Message")), _
    .EventId = x.Element("EventId")}).Distinct

However, the .Distinct doesn’t do what I want, it still shows all combinations of “ApplicationName” and “EventId”.

What I need in the end is a distinct list of results, in a new object with the application name, and event id from the XML.

The “GetAPplicationName” is a function that parses the value looking for a regex match.

Any pointers?

Sample XML

<ROOT>
  <ROW>
    <EventId>1</EventId>
    <CreatedTimestamp>2009-10-28</CreatedTimestamp>
    <Message>There is a bunch
    of 
  garbled
inforamtion here and I'm trying to parse out a value 
Virtual Path: /MyPath then it continues on with more junk after the
message, including extra stuff
    </Message>
    <!--Other elements removed for brevity -->
  </ROW>
  <ROW>
    <EventId>1</EventId>
    <CreatedTimestamp>2009-10-28</CreatedTimestamp>
    <Message>
      There is a bunch
      of
      garbled
      inforamtion here and I'm trying to parse out a value
      Virtual Path: /MyPath then it continues on with more junk after the
      message, including extra stuff
    </Message>
    <!--Other elements removed for brevity -->
  </ROW>
</ROOT>

From here I want the distinct /MyPath and EventId (In this case 1 entry with /MyPath and 1.

My GetApplicationNameMethod, on this sample will return /MyPath

  • 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-12T17:32:37+00:00Added an answer on May 12, 2026 at 5:32 pm

    Distinct doesn’t know how to compare your items, so it returns all the items unfiltered. You should use the Distinct overload that implements IEqualityComparer. This would allow you to compare the ApplicationName and EventId properties to determine equality. However, doing so would mean having a real class, not an anonymous type. The documentation demonstrates how to achieve this in an easy to understand manner.

    EDIT: I was able to use your sample with the IEqualityComparer and an EventInfo class. I added my own implementation of GetApplicationName to test.

        Dim results = (From x In doc.Descendants.Elements("ROW") _
           Select New EventInfo With {.ApplicationName = GetApplicationName(x.Element("Message")), _
            .EventId = x.Element("EventId")})
    
        Console.WriteLine("Total: {0}", results.Count)
        Console.WriteLine("Distinct Total: {0}", results.Distinct.Count)
        Console.WriteLine("Distinct (w/comparer) Total: {0}", results.Distinct(New EventInfoComparer()).Count)
    

    This outputs:

    Total: 2
    Distinct Total: 2
    Distinct (w/comparer) Total: 1
    

    The rest of the code:

    ' EventInfo class and comparer
    Private Function GetApplicationName(ByVal element As XElement)
        Return Regex.Match(element.Value, "Virtual\sPath:\s/(\w+)").Groups(1).Value
    End Function
    
    Public Class EventInfo
        Private _applicationName As String
        Public Property ApplicationName() As String
            Get
                Return _applicationName
            End Get
            Set(ByVal value As String)
                _applicationName = value
            End Set
        End Property
    
        Private _eventId As Integer
        Public Property EventId() As Integer
            Get
                Return _eventId
            End Get
            Set(ByVal value As Integer)
                _eventId = value
            End Set
        End Property
    End Class
    
    Public Class EventInfoComparer
        Implements IEqualityComparer(Of EventInfo)
    
        Public Function Equals1(ByVal x As EventInfo, ByVal y As EventInfo) As Boolean _
            Implements IEqualityComparer(Of EventInfo).Equals
    
            ' Check whether the compared objects reference the same data.
            If x Is y Then Return True
    
            ' Check whether any of the compared objects is null.
            If x Is Nothing OrElse y Is Nothing Then Return False
    
            ' Check whether the EventInfos' properties are equal.
            Return (x.ApplicationName = y.ApplicationName) AndAlso (x.EventId = y.EventId)
        End Function
    
        Public Function GetHashCode1(ByVal eventInfo As EventInfo) As Integer _
            Implements IEqualityComparer(Of EventInfo).GetHashCode
    
            ' Check whether the object is null.
            If eventInfo Is Nothing Then Return 0
    
            ' Get the hash code for the ApplicationName field if it is not null.
            Dim hashEventInfoAppName = _
                If(eventInfo.ApplicationName Is Nothing, 0, eventInfo.ApplicationName.GetHashCode())
    
            ' Get the hash code for the EventId field.
            Dim hashEventInfoId = eventInfo.EventId.GetHashCode()
    
            ' Calculate the hash code for the EventInfo.
            Return hashEventInfoAppName Xor hashEventInfoId
        End Function
    End Class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have run into a very interesting problem trying to debug my custom PHP
I came across a very interesting book. I have done some 2d games but
I have a very interesting task that I need help with. The description is
I have found this thread which describes a very interesting OnSessionStart event, but I
Recently scalaz caught my eye. It looks very interesting, but I have not found
Well I have a question. On Zend Framework We use a very interesting structure
i have a very interesting problem. background i've used TTreeView for years but only
I have one very interesting question (for me :) ).. I'm using jquery and
I found this question very interesting : Programmatic Bot Detection I have a very
I faced a very interesting fact, and hope you have an answer for that.

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.