0

I want to compare all of my list of generic T property value to my local variable searchText.

i already try: x.GetType().GetProperties().All(props => props.GetValue(x).ToString().ToLower().Contains(searchText.ToLower()))

and i get error : NullReferenceException: Object reference not set to an instance of an object.

enter image description here

here my full method :

protected List<T> ProcessCollection<T>(List<T> lstElements, IFormCollection requestFormData, Func<T, IComparable> getProp)
{
    string searchText = string.Empty;
    Microsoft.Extensions.Primitives.StringValues tempOrder = new[] { "" };
    if (requestFormData.TryGetValue("search[value]", out tempOrder))
    {
        searchText = requestFormData["search[value]"].ToString();
    }
    var skip = Convert.ToInt32(requestFormData["start"].ToString());
    var pageSize = Convert.ToInt32(requestFormData["length"].ToString());
    if (requestFormData.TryGetValue("order[0][column]", out tempOrder))
    {
        var columnIndex = requestFormData["order[0][column]"].ToString();
        var sortDirection = requestFormData["order[0][dir]"].ToString();
        tempOrder = new[] { "" };
        if (requestFormData.TryGetValue($"columns[{columnIndex}][data]", out tempOrder))
        {
            var columName = requestFormData[$"columns[{columnIndex}][data]"].ToString();

            if (pageSize > 0)
            {
                var prop = GetProperty<T>(columName);
                if (sortDirection == "asc")
                {
                    return lstElements.Where(x=>x.GetType().GetProperties().All(props => props.GetValue(x).ToString().ToLower().Contains(searchText.ToLower()))
                        .Skip(skip).Take(pageSize).OrderBy(b => prop.GetValue(b)).ToList();
                }
                else
                    return lstElements
                        .Where(x => getProp(x).ToString().ToLower().Contains(searchText.ToLower()))
                        .Skip(skip).Take(pageSize).OrderByDescending(b => prop.GetValue(b)).ToList();
            }
            else
                return lstElements;
        }
    }
    return null;
}

this is how i call my method :

var listItem = ProcessCollection<Jtabel>(temp,requestFormData,x=>x.Name);

the temp that i pass to method already filled and this is the class type that i pass to this method

public class Jtabel : BaseModel
{
    public string Creator { get; set; }
    public string Name { get; set; }
    public int SO { get; set; }
    public int SOD { get; set; }
    public int SAP { get; set; }
    public int BA { get; set; }
    public int Invoice { get; set; }
    public int EPD { get; set; }
    public double DP { get; set; }
}

i will try to explain more detail as much i can, so i want to get all of my property from Jtabel class to my ProcessCollection where inside the method all of Jtable property will test each element that's contain searchText.

Alex
  • 2,931
  • 6
  • 15
  • 35
Dimas
  • 11
  • 3
  • `props.GetValue(x)` requires access to a _getter_ or _indexer_. filter out these properties and perhaps also check access modifiers –  Jul 03 '19 at 18:51
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – JSteward Jul 03 '19 at 19:01
  • Can we get an example of an object you use as `T` ? –  Jul 03 '19 at 19:09
  • 1
    done i already updated my post sorry for my bad english – Dimas Jul 03 '19 at 19:17
  • I would suggest understanding what [`Enumerable.All`](https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.all?view=netframework-4.7.2#System_Linq_Enumerable_All__1_System_Collections_Generic_IEnumerable___0__System_Func___0_System_Boolean__) does would help. Read the documentation. Perhaps you meant to use _projection_, which would be [`Enumerable.Select`](https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.select?view=netframework-4.7.2)? – NetMage Jul 03 '19 at 21:14
  • i already use All() but that give me null NullReferenceException, first i get the type that i pass into method by use x.GetType() and then i get all the properties by using GetProperties() and each properties will test each element that's contain searchText. `return lstElements.Where(x=>x.GetType().GetProperties().All(props =>props.GetValue(x).ToString().ToLower().Contains(searchText.ToLower())) .Skip(skip).Take(pageSize).OrderBy(b => prop.GetValue(b)).ToList();` – Dimas Jul 04 '19 at 01:48

0 Answers0