Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Generic linq contains

Generic linq contains

Scheduled Pinned Locked Moved LINQ
csharplinqtutorialquestion
3 Posts 2 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 4154930
    wrote on last edited by
    #1

    Hi, i have this

    Public Shared Function Filtro(ByVal Company As List(Of Entities.Company), ByVal V As String) As List(Of Entities.Company)
    Try

            Dim S As New Entities.Empresa
            Dim listS As New List(Of Entities.Empresa)
    
            Dim lfilter = From comp In Company \_
                         Where comp.Name.ToLower.Contains(LCase(V))
                         Select comp
    
            For Each item In lfilter
                S = New Entities.Company
                S.ID = item.ID
                S.Name= item.Name
                listS.Add(S)
            Next
    
            Return listS
    
        Catch ex As Exception
            Throw ex
        End Try
    

    this work fine but i have create one of this for everyone entity in my proyect, example one from company, one from employee, exist some generic metod to send the entity and create the filter? sorry my english is poor thanks

    N 1 Reply Last reply
    0
    • U User 4154930

      Hi, i have this

      Public Shared Function Filtro(ByVal Company As List(Of Entities.Company), ByVal V As String) As List(Of Entities.Company)
      Try

              Dim S As New Entities.Empresa
              Dim listS As New List(Of Entities.Empresa)
      
              Dim lfilter = From comp In Company \_
                           Where comp.Name.ToLower.Contains(LCase(V))
                           Select comp
      
              For Each item In lfilter
                  S = New Entities.Company
                  S.ID = item.ID
                  S.Name= item.Name
                  listS.Add(S)
              Next
      
              Return listS
      
          Catch ex As Exception
              Throw ex
          End Try
      

      this work fine but i have create one of this for everyone entity in my proyect, example one from company, one from employee, exist some generic metod to send the entity and create the filter? sorry my english is poor thanks

      N Offline
      N Offline
      nlarson11
      wrote on last edited by
      #2

      You could use an interface to generically check the property that all the entities share. So you could do something like this:

      Public Class Company
      Implements IFilter

      Public Property Name As String Implements IFilter.Name
      Public Property ID As String
      

      End Class

      Public Class Employee
      Implements IFilter

      Public Property Name As String Implements IFilter.Name
      Public Property ID As String
      

      End Class

      Public Interface IFilter
      Property Name As String
      End Interface

      Public Class EntityFilter(Of t)
      Public Function Filter(ByVal entity As List(Of t), ByVal FilterString As String) As List(Of t)
      FilterString = FilterString.ToLower

          Return (From e As t In entity Where CType(e,IFilter).Name.ToLower.Contains(FilterString)).ToList
      
          '' OR ''
      
          Return entity.Where(Function(e) CType(e, IFilter).Name.ToLower.Contains(FilterString)).ToList
      End Function
      

      End Class

      'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous

      U 1 Reply Last reply
      0
      • N nlarson11

        You could use an interface to generically check the property that all the entities share. So you could do something like this:

        Public Class Company
        Implements IFilter

        Public Property Name As String Implements IFilter.Name
        Public Property ID As String
        

        End Class

        Public Class Employee
        Implements IFilter

        Public Property Name As String Implements IFilter.Name
        Public Property ID As String
        

        End Class

        Public Interface IFilter
        Property Name As String
        End Interface

        Public Class EntityFilter(Of t)
        Public Function Filter(ByVal entity As List(Of t), ByVal FilterString As String) As List(Of t)
        FilterString = FilterString.ToLower

            Return (From e As t In entity Where CType(e,IFilter).Name.ToLower.Contains(FilterString)).ToList
        
            '' OR ''
        
            Return entity.Where(Function(e) CType(e, IFilter).Name.ToLower.Contains(FilterString)).ToList
        End Function
        

        End Class

        'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous

        U Offline
        U Offline
        User 4154930
        wrote on last edited by
        #3

        Thanks, i will try, apologizes for the delay but i dont have internet until today thanks for you time.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups