Dynamically add/remove filter in Linq

Posts   
 
    
Posts: 19
Joined: 11-Jan-2010
# Posted on: 22-Sep-2010 22:38:16   

Hello everybody,

Could you please help me with the following issue: I have the following function where filter is an object with a few properties. If properties have some values I have to filter against them. Can I somehow dynamically add Where clause to Linq statement? for example: if filter.Name <> String.Empty -> Where ac.Name = filter.Name (I'm a VB person, but not C# :-) ) and if filter.ActivityIdList isnot Nothing andalso filter.ActivityIdList.Count > 0 then I have to add condition for ac.ActivityId field.

Public Shared Function GetFieldTripDisplayList(ByVal filter As SearchCriteria) As List(Of FieldtripEntity) Using da As IDataAccessAdapter = GetAdapter() Dim md As New LinqMetaData(da)

  If filter Is Nothing Then
Dim res = From ac In md.Fieldtrip.WithPath(
New PathEdge(Of FieldtripEntity)(FieldtripEntity.PrefetchPathFieldtripactivity),
                    New PathEdge(Of FieldtripEntity)(FieldtripEntity.PrefetchPathFieldtripstage))
    Order By ac.Name Select ac

    Return res.ToList
Else                
  Return Nothing
End If

End Using End Function

Thank you very much in advance, Galina.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Sep-2010 05:35:24   

You are looking for PredicateBuilder. Please see this: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=14144&StartAtMessage=0&#78965

David Elizondo | LLBLGen Support Team
Posts: 19
Joined: 11-Jan-2010
# Posted on: 23-Sep-2010 13:46:47   

David, thanks a lot. This is exactly what I was looking for! You are sooo helpful all the time simple_smile . Best, Galina.