简体   繁体   中英

Linq to Collection - Dynamic Where Clause

I have a WPF applicaion of which i would like to populate a treeview. Before this happens I would like the user to be able to select any of the fields/properties available in the collection from a drop down that will then populate the tree with data and grouped appropriately.

EXAMPLE Object

public class class_01{
    public string Property_A {get; set;}
    public string Property_B {get; set;}
    public string Property_C {get; set;}
}

EXAMPLE Collection

List<class_01> list_01 = new List<class_01>();

Again, the drop down will be bound with what ever properties are available from the list. This way if the list where to change the application would not require modification. This is a big requirement for the answer i need.

Lets say the user selects "Property_A".

I would like a linq query method that looks something like this.

Linq Query

    public groupingModel getGrouping(string groupBy) // groupby equals property A in this example
    {

        List<class_01> list = getList(); //Returns the list of data of type class_01 

        var q = from x in w where ????? == groupBy select x; // I dont want to specify a specific property but rather have one applied dynamically

        return q;
    }

I have a custom object that the query would then be parsed into. that looks similar to the following.

Custom Object

public class class_02{
    public string header {get; set;} // will be set to the discrete values of the selected groupby property
    public List<class_01> prop_a {get; set;}
}

This would then be bound to the tree appropriately.

Any Thoughts?

EDIT

Additionally how would I get a list of the unique value for the property the user selects.

for example

{a = 1, b =2, c =3}, {a = 2, b = 3, c = 4}

if the user decides to group on property "a" how would we produce a collection of [1,2]?

This will be needed to construct a where clause.

foreach(value of user selected property){
    string whereClause = string.format("{0} = {1}",selected property, value")
}

EDIT - Catching exception from Dynamic Query

 public List<groupingModel> getGrouping(string groupBy)
        {
            List<groupingModel> lgm = new List<groupingModel>();

            //Categories.Select(c => c.Id).ToList()
            var w2 = getWVWellsList();
            //var v = w2.Select(groupBy).Distinct().Cast<string>().ToArray();
            var v = w2.Select(groupBy).Distinct();

            foreach(string val in v)
            {

                string whereClause = string.Format("{0} = {1}", groupBy, val);

                try
                {

                    IEnumerable<WVWellModel> q2 = w2.Where(whereClause);
                    List<WVWellModel> l = q2.ToList<WVWellModel>();

                    lgm.Add(new groupingModel { Header = val, Wells = l });
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message, "Query Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    throw new Exception("Generic Exception - Issue With Group By Query", e);
                }

            }

            return lgm;
        }

Exception "No Property or field "Colorado" Exists in type WVWellModel"

In the case of this example i can confirm that my where clause was "State = Colorado". It appears the query is applying the value as opposed to the property state which is apart of that type. It is as if it is reversed when the query is called.

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