none
LINQ query help - NOTIN/NOTLIKE RRS feed

  • Question

  • Hi,

    I have linq query (below), I want to execulde if ID: 10,11,12

    Is this query correct?

    Dim Sample = From smp In SomeObject.SomeList _
    Where smp.Visible = True AndAlso smp.ID <> 10 AndAlso smp.ID <> 11 AndAlso smp.ID <> 12 _
            Select smp

    Also,
    Is can I use NOTIN in linq? NOT IN = (10,11,12)
    Thursday, April 15, 2010 2:12 PM

Answers

  • To get IN behavior you use the contains method on an array, e.g.

    Dim Ids() = { 10, 11, 12 }

    Dim Sample = From Smp in SomeObject.SomeList _

      Where Smp.Visible AndAlso Ids.Contains(Smp.ID)

       Select Smp

    To get NOT IN just put Not before Ids.Contains.

    [)amien

    • Proposed as answer by visert Wednesday, April 21, 2010 9:11 AM
    • Marked as answer by Alex LiangModerator Thursday, April 22, 2010 6:12 AM
    Thursday, April 15, 2010 2:48 PM
    Moderator