score:2

Accepted answer

reference this library:

using system.linq.dynamic;

and make your query like that:

string columnname = "active";

var lstcontacts = lstcontacts.where(columnname + " == true");

here is a working example:

using system;
using system.collections.generic;
using system.linq;
using system.linq.dynamic;

public class program
{
    public static void main()
    {
        var lstcontacts = new list<contact>{
            new contact{id = 1, active = true, name = "chris"}, 
            new contact{id = 2, active = true, name = "scott"}, 
            new contact{id = 3, active = true, name = "mark"}, 
            new contact{id = 4, active = false, name = "alan"}};

        string columnname = "active";
        list<contact> results = lstcontacts.where(string.format("{0} == true", columnname)).tolist();
        foreach (var item in results)
        {
            console.writeline(item.id.tostring() + " - " + item.name.tostring());
        }
    }
}

public class contact
{
    public int id
    {
        get;
        set;
    }

    public bool active
    {
        get;
        set;
    }

    public string name
    {
        get;
        set;
    }
}

you can experiment with this .net-fiddle-here


Related Query

More Query from same tag