【问题标题】:Linq to Collection - Dynamic Where ClauseLinq to Collection - 动态 Where 子句
【发布时间】:2017-01-20 21:45:16
【问题描述】:

我有一个 WPF 应用程序,我想填充一个树视图。在此之前,我希望用户能够从下拉列表中选择集合中可用的任何字段/属性,然后使用数据填充树并进行适当的分组。

示例对象

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

示例集合

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

同样,下拉列表将与列表中可用的任何属性绑定。这样,如果更改应用程序的列表不需要修改。这是我需要的答案的一个很大的要求。

假设用户选择“Property_A”。

我想要一个看起来像这样的 linq 查询方法。

Linq 查询

    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;
    }

我有一个自定义对象,然后会将查询解析为该对象。看起来类似于以下内容。

自定义对象

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;}
}

这将被适当地绑定到树上。

有什么想法吗?

编辑

另外,我将如何获取用户选择的属性的唯一值列表。

例如

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

如果用户决定对属性“a”进行分组,我们将如何生成 [1,2] 的集合?

这将是构造 where 子句所必需的。

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

编辑 - 从动态查询中捕获异常

 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;
        }

例外 “WVWellModel 类型中不存在任何属性或字段“Colorado””

在这个例子中,我可以确认我的 where 子句是“State = Colorado”。看起来查询正在应用该值,而不是属于该类型的属性状态。就好像在调用查询时它被反转了一样。

【问题讨论】:

    标签: c# wpf linq


    【解决方案1】:

    【讨论】:

    • 啊哈!这非常方便。你会如何建议我为动态查询生成一个可能值的列表?或者有没有办法在图书馆里处理这个问题?我已经编辑了我的问题以包括该步骤。感谢您输入@Tito。
    • 我已编辑我的问题以使用动态 Linq 查询。我建立了一个字符串 where 子句虽然面临一些问题。抛出异常,指示正在应用错误的字符串属性。
    猜你喜欢
    • 1970-01-01
    • 2016-06-21
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多