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