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. C#
  4. Problem with list with generic filter predicate.

Problem with list with generic filter predicate.

Scheduled Pinned Locked Moved C#
4 Posts 2 Posters 0 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.
  • D Offline
    D Offline
    ddecoy
    wrote on last edited by
    #1

    Consider this example that works:

    List<String> list = new List<string>(){"1","2","a","b"};
    list = list.FindAll(delegate(String s)
    {
    if (s != "a")
    { return false; }
    return true;
    });

    I can't understand why we can't do this :

    List<T> list = (from c in oq select c).ToList();
    list = list.FindAll(delegate(T o)
    {
    if (o.GetType().GetProperty("PropertyName").GetValue(o,null).ToString() != company)
    { return false; }
    return true;
    });

    The 'propertyName' is a property that exists in the objects naturally. This compiles fine but throws an object null reference exception... Thank you for time...

    K 1 Reply Last reply
    0
    • D ddecoy

      Consider this example that works:

      List<String> list = new List<string>(){"1","2","a","b"};
      list = list.FindAll(delegate(String s)
      {
      if (s != "a")
      { return false; }
      return true;
      });

      I can't understand why we can't do this :

      List<T> list = (from c in oq select c).ToList();
      list = list.FindAll(delegate(T o)
      {
      if (o.GetType().GetProperty("PropertyName").GetValue(o,null).ToString() != company)
      { return false; }
      return true;
      });

      The 'propertyName' is a property that exists in the objects naturally. This compiles fine but throws an object null reference exception... Thank you for time...

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      The second code is very convoluted, I don't know why you are using reflection to get the property type.

      List list = (from c in oq select c).ToList();
      list = list.FindAll(o => o.GetType().GetProperty("PropertyName").GetValue(o,null).ToString() == company));

      is cleaner, but you can do the find all operation as where clause in the select. The problem you are currently getting is either because T doesn't have a propertyname (and if it does, why are you getting it by refelection?) or the property istelf has a null value.

      Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

      D 1 Reply Last reply
      0
      • K Keith Barrow

        The second code is very convoluted, I don't know why you are using reflection to get the property type.

        List list = (from c in oq select c).ToList();
        list = list.FindAll(o => o.GetType().GetProperty("PropertyName").GetValue(o,null).ToString() == company));

        is cleaner, but you can do the find all operation as where clause in the select. The problem you are currently getting is either because T doesn't have a propertyname (and if it does, why are you getting it by refelection?) or the property istelf has a null value.

        Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

        D Offline
        D Offline
        ddecoy
        wrote on last edited by
        #3

        Keith Barrow wrote:

        The problem you are currently getting is either because T doesn't have a propertyname (and if it does, why are you getting it by refelection?) or the property istelf has a null value.

        That seems impossible because T has always got 'propertName' property and the value is never null because it's part of a primary key.

        Keith Barrow wrote:

        you can do the find all operation as where clause in the select.

        Maybe I didn't show enough code to make clear why I can't do the 'FindAll' in the 'Where' clause ...

        var oq = (ObjectQuery<T>)(p.GetValue(EntityObject, null));
        List<T> list = (from c in oq select c).ToList();

        The type of the ObjectQuery is only known in runtime because I get a specific ObjectQuery thru reflection in runtime. so

        from c in oq select c where c.'no properties here' select c

        ...

        D 1 Reply Last reply
        0
        • D ddecoy

          Keith Barrow wrote:

          The problem you are currently getting is either because T doesn't have a propertyname (and if it does, why are you getting it by refelection?) or the property istelf has a null value.

          That seems impossible because T has always got 'propertName' property and the value is never null because it's part of a primary key.

          Keith Barrow wrote:

          you can do the find all operation as where clause in the select.

          Maybe I didn't show enough code to make clear why I can't do the 'FindAll' in the 'Where' clause ...

          var oq = (ObjectQuery<T>)(p.GetValue(EntityObject, null));
          List<T> list = (from c in oq select c).ToList();

          The type of the ObjectQuery is only known in runtime because I get a specific ObjectQuery thru reflection in runtime. so

          from c in oq select c where c.'no properties here' select c

          ...

          D Offline
          D Offline
          ddecoy
          wrote on last edited by
          #4

          Ok so it was the 'propertyName' that was incorrect, my mistake, sorry ! All is well :) and thank you for your input. :doh:

          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