繁体   English   中英

具有分组依据和字符串变量的LINQ动态查询

[英]LINQ Dynamic Query with group by and string variable

我想用LINQ-to-SQL创建一个动态查询,除一件事情外,其他所有东西都起作用: 我查看了其他问题,但到目前为止还没有找到不起作用的原因。

我使用using System.Linq.Dynamic; 使用以下查询(有效):

int numberOfRankedItems = 3;

string minMaxChoice = "max";
string orderBy = "";
if (minMaxChoice == "max")
{
    orderBy = "queryGroup.Sum(row => row.POSITIONVALUE) descending";
} else if (minMaxChoice == "min")
{
    orderBy = "queryGroup.Sum(row => row.POSITIONVALUE) ascending";
}

string minMaxFeatureColumnName = "BRANDNAME";
string selectMinMaxFeature = "new { minMaxFeature = queryGroup.Key." + minMaxFeatureColumnName + ", sumPosition = queryGroup.Sum(row => row.POSITIONVALUE)}";

var query = (from tableRow in Context.xxx
                where /* some filters */
                group tableRow by tableRow.BRANDNAME into queryGroup
                orderby orderBy
                select selectMinMaxFeature).Take(numberOfRankedItems).ToList();

将变量用于orderbyselect可以很好地工作,但是对于group by无论我尝试用变量替换什么,都可以。 如果我例如使用变量代替tableRow.BRANDNAME ,则查询实际上可以工作,但是查询返回错误的结果。 目标是能够动态设置分组的列。

想法?

谢谢

编辑:其他变量似乎也存在多个问题。 因此,我对此问题进行了概括:如何将以下查询归纳为:

  1. 分组依据的列
  2. 按+ ASC或DESC排序的列
  3. 在select语句中,第一个语句的列名(BRANDNAME)

这是查询:

var query = (from tableRow in ctx.xxx
                where /* filter */
                group tableRow by new { tableRow.BRANDNAME } into queryGroup
                orderby queryGroup.Sum(row => row.POSITIONVALUE) descending
                select new { minMaxFeature = queryGroup.Key.BRANDNAME, sumPosition = queryGroup.Sum(row => row.POSITIONVALUE) }).Take(numberOfRankedItems).ToList();

没有表情树会很好;)

我现在已经阐述了该问题的解决方案:

该解决方案using System.Linq.Dynamic;之前的用法using System.Linq.Dynamic; 现在允许设置某些变量。 最后,它返回一个字典。

// set variables
int numberOfRankedItems;
if (queryData.NumberOfRankedItems != null)
{
    numberOfRankedItems = (int) queryData.NumberOfRankedItems;
} else
{
    numberOfRankedItems = 0;
}
string minMaxChoice = queryData.MinMaxChoice;
string minMaxFeatureColumnName = queryData.MinMaxFeatureColumnName;

string orderByPrompt = "";
if (minMaxChoice == "max")
{
    orderByPrompt = "it.Sum(POSITIONVALUE) descending";
} else if (minMaxChoice == "min")
{
    orderByPrompt = "it.Sum(POSITIONVALUE) ascending";
}

string groupByPrompt = queryData.MinMaxFeatureColumnName;

// execute query
IQueryable query = (from tableRow in Context.xxx
                        where /* some filters */
                        select tableRow).
                        GroupBy(groupByPrompt, "it").
                        OrderBy(orderByPrompt).
                        Select("new (it.Key as minMaxFeature, it.Sum(POSITIONVALUE) as sumPosition)").
                        Take(numberOfRankedItems);

// create response dictionary
Dictionary<int?, Dictionary<string, int?>> response = new Dictionary<int?, Dictionary<string, int?>>();
int count = 1;
foreach (dynamic item in query)
{
    string minMaxFeatureReturn = item.GetType().GetProperty("minMaxFeature").GetValue(item);
    int? sumPositionReturn = item.GetType().GetProperty("sumPosition").GetValue(item);
    response[count] = new Dictionary<string, int?>() { { minMaxFeatureReturn, sumPositionReturn } };
    count++;
}            
return response;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM