locked
Generic method for common search RRS feed

  • Question

  • User-595461256 posted

    Hi,

    Thanks in Advance.

    I need to filter out list of records in the given date range.I have to do for more than 10 screens but logic is same.

    I am planning to create one generic method like

    Public List<T> FilterRecords(List<T> inputtype,DateTime StartDate,DateTime StartDate)
    {
    //my code logic
    }

    Please assist me how to create generic method like above and more specifically I have to return result as what inputtype parameter passed.

    Wednesday, January 30, 2019 12:10 PM

All replies

  • User753101303 posted

    Hi,

    A useful trick I saw once is starting by writing client code (that doesn't work or runs against a no op implementation) rather than starting by actually implementing it even before having looked at how it will be used. So in your app you want to be able to write something such as :

    myData=myData.Between(o=>o.MyColumn,startDate,endDate);

    - behing the scene it would do a where(o=>o.MyColumn>startDate && o.Mycolumn<endDate.AddDays(1)); is this even the first problem you have in doing something general for 10 screens ?

    It seems for now your design is :

    myData=FilterRecords(MyData,startDate,endDate); which raises :

    - how do you know which field is filtered ?
    - is this automatic if a single data/time property is found ?
    - does it throw if multiple fields are found ?
    - and in this case do you want to be able to optional pass the property etc...
    - or maybe you have an interface to expose which date property is used for filtering etc...
    - FilterRecords is not easily discoverable

    Wednesday, January 30, 2019 12:40 PM
  • User475983607 posted

    Hi,

    Thanks in Advance.

    I need to filter out list of records in the given date range.I have to do for more than 10 screens but logic is same.

    I am planning to create one generic method like

    Public List<T> FilterRecords(List<T> inputtype,DateTime StartDate,DateTime StartDate)
    {
    //my code logic
    }

    Please assist me how to create generic method like above and more specifically I have to return result as what inputtype parameter passed.

    One approach is inheriting a base class or interface 

        public class DateFilterBase
        {
            public DateTime StartDate { get; set; }
            public DateTime EndDate { get; set; }
        }
            public static List<T> FilterRecords<T>(List<T> inputtype, DateTime StartDate, DateTime EndDate) where T : DateFilterBase
            {
               var results =  inputtype.Where(m => m.StartDate == StartDate & m.EndDate == EndDate);
               return results.ToList();
            }

    Another approach is passing a lambda.

    https://stackoverflow.com/questions/1299534/linq-passing-lambda-expression-as-parameter-to-be-executed-and-returned-by-meth

    Wednesday, January 30, 2019 12:52 PM
  • User1520731567 posted

    Hi pughal_dotmvc,

    Making a generic helper method you are rewriting existing framework code and you open yourself to performance and maintenance issues while also reducing code readability:

    https://stackoverflow.com/questions/5880056/make-a-search-method-generic-using-linq

    Here is a complete working sample how to pass LINQ expression as a parameter:

    https://stackoverflow.com/a/3664772

    Best Regards.

    Yuki Tao

    Thursday, January 31, 2019 8:33 AM