C# 实体框架自定义查询条件

C# 实体框架自定义查询条件,c#,sql,asp.net,entity-framework,C#,Sql,Asp.net,Entity Framework,我有 除了按c.issue\u date进行分组外,还有其他字段,如“c.gender”、“c.termination\u date”、“c.customer\u name”和许多字段。我想创建一个控制台应用程序,用户可以在其中输入他想要分组依据的字符串,然后查询将获取该字符串。差不多 var query = from c in db.customers_info group c by c.issue_date into g

我有

除了按c.issue\u date进行分组外,还有其他字段,如“c.gender”、“c.termination\u date”、“c.customer\u name”和许多字段。我想创建一个控制台应用程序,用户可以在其中输入他想要分组依据的字符串,然后查询将获取该字符串。差不多

var query = from c in db.customers_info
                            group c by c.issue_date into g
                            select new
                            {
                                name = g.Key,
                                value = g.Count()
                            };
到目前为止,我的次优解决方案是:

var query = from c in db.customers_info
                            group c by c.{user_input} into g
                            select new
                            {
                                name = g.Key,
                                value = g.Count()
                            };
可能吗


编辑:希望将groupby作为生成的SQL查询的一部分进行处理,而不是将所有字段带入应用程序,并在应用程序级别处理groupby。谢谢

可能是这样的:

var query = from c in db.customers_info
                            group c by user_input.Equals("issue_date")? c.issue_date : user_input.Equals("gender")? c.gender : ... and more conditions.
                            into g
                            select new
                            {
                                name = g.Key,
                                value = g.Count()
                            };
摘自本文:

这是否回答了您的问题?嘿,@gunr217谢谢你的评论。看起来他们的解决方案将“groupBy”带入了应用程序级别。我更喜欢在数据库中执行“GroupBy”。Linq将把GroupBy条件作为它生成的SQL查询中的一个子句来实现。这就是您在数据库中执行“GroupBy”的意思吗?您可以使用dbContext的Database.Log属性来检查Linq生成的SQL。中的一个答案建议使用动态Linq。我相信这可以正确地动态生成查询,请参阅:动态LINQGroupBy功能的工作原理与普通的强类型GroupBy相同。请参阅:。如果您使用的是.net framework而不是.net core,请签出
/*  
 *  Listing 1
 *  Do TRY this at home, this code IS meant to be compiled
 */

var keySelectors = new List<Expression<Func<Measurement, ReportResult>>>();

/* list of select keys and aggregates that resulting groups will be projected to */
var selectAggregate = new List<Expression<Func<IGrouping<ReportResult, Measurement>, ReportResult>>>();

if (groupByClient)
{
    /* if grouping by client is selected, then we will not only group by that key
     * but will also add it to the projection */
    keySelectors.Add(x => new ReportResult { Client = x.Client });
    selectAggregate.Add(x => new ReportResult { Client = x.Key.Client });
}

if (groupByUser)
{
    /* same as before, add it to key selectors and add it to the projection */
    keySelectors.Add(x => new ReportResult { FirstUsername = x.User.UserName });
    selectAggregate.Add(x => new ReportResult { FirstUsername = x.Key.FirstUsername });
}

if (groupByClient || groupByUser)
{
    /* if either of the grouping is selected, then add the following aggregator */
    selectAggregate.Add(x => new ReportResult { FirstReading = x.Sum(x => x.Reading) });
}

var result = _context.Measurement
    /* call the magic function */
    .GroupBy(LinqExpressionHelpers.CombineKeySelectors(keySelectors))
    /* call the magic function again in a different role */
    .Select(LinqExpressionHelpers.CombineKeySelectors(selectAggregate));