Generic linq contains
-
Hi, i have this
Public Shared Function Filtro(ByVal Company As List(Of Entities.Company), ByVal V As String) As List(Of Entities.Company)
TryDim 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
-
Hi, i have this
Public Shared Function Filtro(ByVal Company As List(Of Entities.Company), ByVal V As String) As List(Of Entities.Company)
TryDim 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
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 IFilterPublic Property Name As String Implements IFilter.Name Public Property ID As String
End Class
Public Class Employee
Implements IFilterPublic Property Name As String Implements IFilter.Name Public Property ID As String
End Class
Public Interface IFilter
Property Name As String
End InterfacePublic Class EntityFilter(Of t)
Public Function Filter(ByVal entity As List(Of t), ByVal FilterString As String) As List(Of t)
FilterString = FilterString.ToLowerReturn (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
-
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 IFilterPublic Property Name As String Implements IFilter.Name Public Property ID As String
End Class
Public Class Employee
Implements IFilterPublic Property Name As String Implements IFilter.Name Public Property ID As String
End Class
Public Interface IFilter
Property Name As String
End InterfacePublic Class EntityFilter(Of t)
Public Function Filter(ByVal entity As List(Of t), ByVal FilterString As String) As List(Of t)
FilterString = FilterString.ToLowerReturn (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
Thanks, i will try, apologizes for the delay but i dont have internet until today thanks for you time.