none
Distinct values for one column in stored procedure from EF RRS feed

  • Question

  • Hi all,

    I would like to populate comboboxes with data from my stored procedure imported with Entity Framework 6 to my application.

    I have two columns there FirstActivity and SecondActivity. I'm executing sp using method:

            Public Sub FillFirstActivity()
                Using context As New TimerConn
                    Dim result = context.sp_Grab_Activities(MRU, MAR, TOW).ToList()
                    For Each act In result
                        _First.Add(act.FirstActivity)
                    Next
                End Using
            End Sub

    _First populates first combobox. Unforutnately, with doubles. sometimes First Activity is assigned to more than one Second activity.

    Distinct is not working here.

    How could I populate first combobox with single items?

    Thank you in advance


    Wednesday, May 27, 2015 5:41 AM

Answers

  • If FirstActivity is a scalar value (for example a string) you could just check if _First contains the string before you add it:

        Public Sub FillFirstActivity()
            Using context As New TimerConn
                Dim result = context.sp_Grab_Activities(MRU, MAR, TOW).ToList()
                For Each act In result
                    If Not (_First.Contains(act.FirstActivity)) Then
                        _First.Add(act.FirstActivity)
                    End If
                Next
            End Using
        End Sub


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by VoVoTT Monday, June 1, 2015 11:33 AM
    Friday, May 29, 2015 8:35 AM

All replies

  • I guess the question would be is why are you even using a sproc to do this? And why can't you use Linq that has a Distiinct() function?
    Wednesday, May 27, 2015 1:34 PM
  • Thank for your response.

    I had that sp in my sql db to get the activities, so I use it here.

    Ok, I've done that with linq:

                    Dim result = (From act In context.tbl_Activities Where (act.MRU = MRU() And act.Market = MAR() And act.Tower = TOW()) Order By act.FirstActivity).Distinct().ToList()
                    For Each acta In result
                        _First.Add(acta)
                    Next

    Still, I get duplicates in my combobox.

    Friday, May 29, 2015 8:09 AM
  • If FirstActivity is a scalar value (for example a string) you could just check if _First contains the string before you add it:

        Public Sub FillFirstActivity()
            Using context As New TimerConn
                Dim result = context.sp_Grab_Activities(MRU, MAR, TOW).ToList()
                For Each act In result
                    If Not (_First.Contains(act.FirstActivity)) Then
                        _First.Add(act.FirstActivity)
                    End If
                Next
            End Using
        End Sub


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by VoVoTT Monday, June 1, 2015 11:33 AM
    Friday, May 29, 2015 8:35 AM