?
Solved

Select an item on the list that is not on the other list.

Posted on 2011-05-05
7
Medium Priority
?
351 Views
Last Modified: 2013-11-11
Hi, I have two list defaultSortOptions and savedSortOptions:

Dim defaultSortOptions as new List(Of SortOption)
Dim savedSortOptions as new List(Of SortOption)

How do I :

1.) Select all sortoption in the defaultSortOptions list that is not in the savedSortoption
2.) Select those sortoption in the defaultSortoption list that is differ in the sortoption text but similar sortoption ID in the savedSortOption lists.

Need to use LINQ in this...

If you need further clarificatio please fill free to ask me.

Regards,

Joseph
Public Class SortOption
    Private _id As Integer
    Private _text As String

    Public Property ID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Public Property Text() As String
        Get
            Return _text
        End Get
        Set(ByVal value As String)
            _text = value
        End Set
    End Property

End Class

Open in new window

0
Comment
Question by:PagodNaUtak
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35698006
Try these:
Dim notIn = From item In defaultSortOptions _
            Where Not savedSortOptions.Contains(item) _
            Select item

Dim matchingId = From item1 In defaultSortOptions _
                 From item2 In savedSortOptions _
                 Where item1.ID = item2.ID _
                 Select item1

Open in new window

0
 
LVL 13

Expert Comment

by:jeebukarthikeyan
ID: 35698044
hi,

try this way tooo


    string[] words = { "Asp", ".net", "php", "vb", "c", "c#", "f#", "c++" };
            string[] wos = { "Asp", ".net", "php", "vb", "c", "c#" };
            var tmp = words.Except(wos);

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35698047
For the "matchingId" query, if you need the text to strictly be different, then you can modify the query to this:
Dim matchingId = From item1 In defaultSortOptions _
                 From item2 In savedSortOptions _
                 Where item1.ID = item2.ID AndAlso item1.Text <> item2.Text _
                 Select item1

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Author Comment

by:PagodNaUtak
ID: 35698506
Hi kaufmed,

it does not work...

Please see below code... for more details...

The output is:

---------------------------
1 Newest to oldest
2 Oldest to newest
3 Most Popular
4 Featured Sort
5 New Order

Dim defaultSortOptions As New List(Of SortOption)
        Dim savedSortOptions As New List(Of SortOption)
        Dim newOrder As New List(Of SortOption)

        defaultSortOptions.Add(New SortOption(1, "Newest to oldest"))
        defaultSortOptions.Add(New SortOption(2, "Oldest to newest"))
        defaultSortOptions.Add(New SortOption(3, "Most Popular"))
        defaultSortOptions.Add(New SortOption(4, "Featured Sort"))
        defaultSortOptions.Add(New SortOption(5, "New Order"))

        savedSortOptions.Add(New SortOption(1, "Newest to oldest"))
        savedSortOptions.Add(New SortOption(2, "Oldest to newest"))
        savedSortOptions.Add(New SortOption(3, "Most Popular"))
        savedSortOptions.Add(New SortOption(4, "By Topic"))

        Dim notIn = From item In defaultSortOptions _
            Where Not savedSortOptions.Contains(item) _
            Select item

        Response.Write("---------------------------")
        Response.Write("<br />")

        For Each sort In notIn
            Response.Write(sort.ID & " " & sort.Text)
            Response.Write("<br />")
        Next

Open in new window

0
 
LVL 12

Accepted Solution

by:
kumar754 earned 668 total points
ID: 35698539
use Except();
eg;  
 
   var excludedSortOptions = defaultSortOptions.Except(savedSortOptions);

or for more advance operations using IEqualityComparer, refer this link and documentation:

http://msdn.microsoft.com/en-us/library/bb336390.aspx
0
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 668 total points
ID: 35699290
Hi PagodNaUtak;

See if this returns what you are looking for.

        Dim defaultSortOptions As New List(Of SortOption)
        Dim savedSortOptions As New List(Of SortOption)
        Dim newOrder As New List(Of SortOption)
        Dim newDiffOrder As New List(Of SortOption)

        defaultSortOptions.Add(New SortOption(1, "Newest to oldest"))
        defaultSortOptions.Add(New SortOption(2, "Oldest to newest"))
        defaultSortOptions.Add(New SortOption(3, "Most Popular"))
        defaultSortOptions.Add(New SortOption(4, "Featured Sort"))
        defaultSortOptions.Add(New SortOption(5, "New Order"))

        savedSortOptions.Add(New SortOption(1, "Newest to oldest"))
        savedSortOptions.Add(New SortOption(2, "Oldest to newest"))
        savedSortOptions.Add(New SortOption(3, "Most Popular"))
        savedSortOptions.Add(New SortOption(4, "By Topic"))

        newOrder = savedSortOptions.Except(defaultSortOptions, New SortOptionComparer()).ToList()
        newDiffOrder = defaultSortOptions.Except(savedSortOptions, New SortOptionDiffComparer()).ToList()


Public Class SortOptionComparer
    Implements IEqualityComparer(Of SortOption)

    Public Function Equals1(ByVal x As SortOption, ByVal y As SortOption) As Boolean Implements System.Collections.Generic.IEqualityComparer(Of SortOption).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 SortOption' properties are equal.
        Return (x.Text.ToUpper() = y.Text.ToUpper())

    End Function

    Public Function GetHashCode1(ByVal obj As SortOption) As Integer Implements System.Collections.Generic.IEqualityComparer(Of SortOption).GetHashCode

        ' Check whether the object is null.
        If obj Is Nothing Then Return 0

        ' Get hash code for the Text field if it is not null.
        Dim hashText = If(obj.Text Is Nothing, 0, obj.Text.GetHashCode())

        ' Calculate the hash code for the SortOption.
        Return hashText

    End Function

End Class

Public Class SortOptionDiffComparer
    Implements IEqualityComparer(Of SortOption)


    Public Function Equals1(ByVal x As SortOption, ByVal y As SortOption) As Boolean Implements System.Collections.Generic.IEqualityComparer(Of SortOption).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 SortOption' properties are equal.
        Return (x.ID = y.ID AndAlso Not x.Text = y.Text)

    End Function

    Public Function GetHashCode1(ByVal obj As SortOption) As Integer Implements System.Collections.Generic.IEqualityComparer(Of SortOption).GetHashCode

        ' Check whether the object is null.
        If obj Is Nothing Then Return 0

        ' Get hash code for the Text field if it is not null.
        Dim hashText = If(obj.Text Is Nothing, 0, obj.Text.GetHashCode())
        ' Get hash code for the ID field if it is not null.
        Dim hashID = If(obj.ID = Nothing, 0, obj.ID.GetHashCode())

        ' Calculate the hash code for the product.
        Return hashID Xor hashText

    End Function

End Class

Open in new window


Fernando
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 664 total points
ID: 35699359
You can use the IEqualityComparer as demonstrated above, or you could override the Equals function:
Public Class SortOption
    Private _id As Integer
    Private _text As String

    Public Sub New(ByVal id As Integer, ByVal text As String)
        Me._id = id
        Me._text = text
    End Sub

    Public Property ID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Public Property Text() As String
        Get
            Return _text
        End Get
        Set(ByVal value As String)
            _text = value
        End Set
    End Property

    Public Overrides Function Equals(ByVal obj As Object) As Boolean
        Dim opt As SortOption = TryCast(obj, SortOption)

        If opt IsNot Nothing Then
            Return opt.ID = Me.ID AndAlso opt.Text = Me.Text
        End If

        Return Me.Equals(obj)
    End Function
End Class

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question