none
how to filter from a generic List using contains RRS feed

  • Question

  • Hai

    I want to filter from a Generic list using contains.

    For this I use  code like this

    public IList<StudentDetail> Vm_NameList { get; set; }

    public IList<StudAddress> Vm_AddressList { get; set; } public StudentVM() { IList<StudentDetail> Vm_NameList = new List<StudentDetail>(); IList<StudAddress> Vm_AddressList = new List<StudAddress>(); Vm_NameList.Add(new StudentDetail { StudentId = 1, StudentName = "Mathew" }); Vm_NameList.Add(new StudentDetail { StudentId = 2, StudentName = "Denny" }); Vm_NameList.Add(new StudentDetail { StudentId = 3, StudentName = "Jiss" }); Vm_NameList.Add(new StudentDetail { StudentId = 4, StudentName = "Don" }); Vm_NameList.Add(new StudentDetail { StudentId = 5, StudentName = "Den" }); var SelectedItem = Filter(Vm_NameList, "StudentName", "Jiss"); var SelectedAddress = Filter(Vm_AddressList,"Add","xxx" ); } private static IEnumerable<T> Filter<T>(IEnumerable<T> entities, string propertyName, string Txt) { var propertyInfo = typeof(T).GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); //Here I want to filter some thing like this //var FilteredItems = entities.Where(c => c.GetType().GetProperties().Select(x=> x.GetValue(c)).ToLower().Contains(Txt.ToLower())).ToList(); return FilteredItems; }

    Here I want to filter using contains with studentname parameter and return the selected list.

    Thanks


    programmer


    • Edited by babucr Saturday, December 7, 2019 7:32 AM
    Saturday, December 7, 2019 7:28 AM

Answers

  • If you still want to use Reflection for some reasons, then try this:

    var FilteredItems = entities.Where( e => propertyInfo.GetValue( e ).ToString( ).IndexOf( Txt, StringComparison.CurrentCultureIgnoreCase) >= 0 );




    • Edited by Viorel_MVP Saturday, December 7, 2019 1:24 PM
    • Marked as answer by babucr Saturday, December 7, 2019 3:08 PM
    Saturday, December 7, 2019 1:19 PM

All replies

  • So why can't you just use a  simple Linq query and query Vm_NameList?

    You're already using an example of a Linq query, which I don't even know what you were trying to do there. :)

    //var FilteredItems = entities.Where(c => c.GetType().GetProperties().Select(x=> x.GetValue(c...……..

    var returnlist = Vm_NameList.Select(a => a.StudentName.Contains("somestring").ToList();


    • Edited by DA924x Saturday, December 7, 2019 1:15 PM
    Saturday, December 7, 2019 1:08 PM
  • If you still want to use Reflection for some reasons, then try this:

    var FilteredItems = entities.Where( e => propertyInfo.GetValue( e ).ToString( ).IndexOf( Txt, StringComparison.CurrentCultureIgnoreCase) >= 0 );




    • Edited by Viorel_MVP Saturday, December 7, 2019 1:24 PM
    • Marked as answer by babucr Saturday, December 7, 2019 3:08 PM
    Saturday, December 7, 2019 1:19 PM
  •  This is I want

    Thanks  Viorel.



    programmer

    Saturday, December 7, 2019 3:08 PM