简体   繁体   中英

Search in IQueryable<object>

Is it possible to search in an IQueryable

public static IQueryable<object> SearchAllFields(IQueryable<object> query, string term)
{
    query = query.Where(q => q.Property1 == term);
    query = query.Where(q => q.Property2 == term);
    query = query.Where(q => q.Property3 == term);

    return query;
}

Lets say I want to compare the search term to each of the properties that the object might have, without knowing before what properties the object might have.


Edit :

I am attempting to create a generic DataTable solution to display any tabular information that might be necessary (orders, books, customers, etc.)

To test the concept I'm using the ApplicationLogs table in my database. The DataTable looks as follows:

在此处输入图片说明

Lets say when typing in that search box I want to search for that value in all the columns that might be displayed. The query that populates the table:

IQueryable<object> query = (from log in db.ApplicationLog
                            orderby log.LogId descending
                            select new
                            {
                                LogId = log.LogId,
                                LogDate = log.LogDate.Value,
                                LogLevel = log.LogLevelId == 1 ? "Information" : log.LogLevelId == 2 ? "Warning" : "Error",
                                LogSource = log.LogSourceId == 1 ? "Www" : log.LogSourceId == 2 ? "Intranet" : "EmailNotification",
                                LogText = log.LogText
                            });

As you can see, this query will determine what the properties of the object will be. The example is taken from the logs table, but it can come from any number of tables. Then, if I want to call the generic search method from the original post:

query = DataTableHelper.SearchAllFields(query, pageRequest.Search);

You can use reflection to search through all the properties of the element, but if you want to return all rows where any property matches then you need to use predicatebuilder to build the applied query instead Where().

This example code will return both instances of Foo where A,B and C are "a". And the instances of Bar where E, F and G are "a". Also added example of anonymous type.

class Program
{
    private class Foo
    {
        public string A { get; set; }
        public string B { get; set; }
        public string C { get; set; }
    }

    private class Bar
    {
        public string E { get; set; }
        public string F { get; set; }
        public string G { get; set; }
    }

    static void Main(string[] args)
    {
        var list = new List<Foo>
        {
            new Foo { A = "a", B = "a", C = "a" },
            new Foo { A = "a2", B = "b2", C = "c2" },
            new Foo { A = "a3", B = "b3", C = "c3" },
        };
        var list2 = new List<Bar>
        {
            new Bar { E = "a", F = "a", G = "a" },
            new Bar { E = "a2", F = "b2", G = "c2" },
            new Bar { E = "a3", F = "b3", G = "c3" },
        };

        var q1 = Filter(list.AsQueryable(), "a");
        var q2 = Filter(list2.AsQueryable(), "a");

        foreach (var x in q1)
        {
            Console.WriteLine(x);
        }

        foreach (var x in q2)
        {
            Console.WriteLine(x);
        }

        var queryable = list.Select(p => new
        {
            X = p.A,
            Y = p.B,
            Z = p.C
        }).AsQueryable();
        var q3 = Filter(queryable, "a"); 
        foreach (var x in q3)
        {
            Console.WriteLine(x);
        }

        Console.ReadKey();
    }

    private static IQueryable<object> Filter(IQueryable<object> list, string value)
    {
        foreach (var prop in list.ElementType.GetProperties())
        {
            var prop1 = prop;
            list = list.Where(l => Equals(prop1.GetValue(l, null), value));
        }

        return list;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM