如何在 Dynamic Linq Core 中 GroupBy nothing

How to GroupBy nothing in Dynamic Linq Core

我正在创建一些动态 linq 语句供用户决定分组依据。但是,当用户没有输入任何内容时,我希望查询按 'nothing' 分组,就像只输出查询结果一样,就好像没有分组一样。类似于此答案 但使用 Dynamic Linq Core。

var query = db.II_POLICY_AND_COVERAGE
                    .Where(x => (x.date>= settings.StartDate && x.date<= settings.EndDate))
                    .GroupBy(user_groupby_input_string);

                if (user_defined_calc_method.Equals("Total"))
                {
                    query = query.Select("new(Key as name, Sum(money_column) as value)");
                }
                else if (user_defined_calc_method.Equals("Count"))
                {
                    query = query.Select("new(Key as name, Count() as value)");
                }

例如,如果 user_groupby_input_string"gender",查询将按性别列分组。如果 user_groupby_input_string"" 或 null,查询将忽略 groupby 行,只对整个数据集求和或计数。

我看起来不正确,你正在使用它 EF,如果不进行修改,这可能会或可能不会工作。

你可以尝试自己做:

第 1 步。创建 'dynamic' 石斑鱼。

class X
{
    public string A { get; set; }
    public int B { get; set; }

    public static Func<X, object> GetGroupBy(string name)
        => name?.ToLower() switch
        {

            "a" => x => x.A,
            "b" => x => x.B,
            _ => x => 1 // or x.GetHashCode() if used for objects in memory
        };

    // public override int GetHashCode() => HashCode.Combine(A, B);
}

第 2 步. 使用它

var groups = .GroupBy(X.GetGroupBy(query))

步骤 3. 测试

var arr = new[] { new X { A = "x", B = 1 }, new X { A = "x", B = 2 }, new X { A = "", B = 1 }, new X { A = "", B = 3 } };

foreach (var query in new[]{ "a", "B", "", null, "7" })
{
    var queryStr = query != null ? $"'{query}'" : "null";
    Console.WriteLine($"Query: {queryStr}");
    var groups = arr.GroupBy(X.GetGroupBy(query));

    foreach (var g in groups)
    {
        var elements = string.Join(",", g.Select(x => $"'{x.A}':{x.B}"));
        Console.WriteLine($"\tGroup: '{g.Key}'\tElements: {elements}");
    }
}
Query: 'a'
    Group: 'x'  Elements: 'x':1,'x':2
    Group: ''   Elements: '':1,'':3
Query: 'B'
    Group: '1'  Elements: 'x':1,'':1
    Group: '2'  Elements: 'x':2
    Group: '3'  Elements: '':3
Query: ''
    Group: '1'  Elements: 'x':1,'x':2,'':1,'':3
Query: null
    Group: '1'  Elements: 'x':1,'x':2,'':1,'':3
Query: '7'
    Group: '1'  Elements: 'x':1,'x':2,'':1,'':3

对于 _ => x => x.GetHashCode() 这将是:

Query: 'a'
    Group: 'x'  Elements: 'x':1,'x':2
    Group: ''   Elements: '':1,'':3
Query: 'B'
    Group: '1'  Elements: 'x':1,'':1
    Group: '2'  Elements: 'x':2
    Group: '3'  Elements: '':3
Query: ''
    Group: '94073102'   Elements: 'x':1
    Group: '142666244'  Elements: 'x':2
    Group: '186235014'  Elements: '':1
    Group: '-46300365'  Elements: '':3
Query: null
    Group: '94073102'   Elements: 'x':1
    Group: '142666244'  Elements: 'x':2
    Group: '186235014'  Elements: '':1
    Group: '-46300365'  Elements: '':3
Query: '7'
    Group: '94073102'   Elements: 'x':1
    Group: '142666244'  Elements: 'x':2
    Group: '186235014'  Elements: '':1
    Group: '-46300365'  Elements: '':3

当您按常量分组时,LINQ Translator 会专门处理这种情况,例如 1。 LINQ Translator 将删除分组并使用聚合函数使简单明了 SQL。