C# 具有group by和字符串变量的LINQ动态查询
我想用LINQ to SQL创建一个动态查询,除了一件事:group by之外,其他一切都可以工作。我查看了其他问题,但到目前为止还没有找到它不起作用的原因 我使用System.Linq.Dynamic;使用以下有效的查询:C# 具有group by和字符串变量的LINQ动态查询,c#,sql,linq,C#,Sql,Linq,我想用LINQ to SQL创建一个动态查询,除了一件事:group by之外,其他一切都可以工作。我查看了其他问题,但到目前为止还没有找到它不起作用的原因 我使用System.Linq.Dynamic;使用以下有效的查询: int numberOfRankedItems = 3; string minMaxChoice = "max"; string orderBy = ""; if (minMaxChoice == "max") { orderBy = "queryGroup.Su
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();
对orderby和select使用变量可以很好地工作,但对groupby来说,无论我尝试用变量替换什么,都不起作用。例如,如果使用变量而不是tableRow.BRANDNAME,但查询返回错误的结果,则查询实际上可以工作。目标是能够动态地为分组设置列
想法
谢谢
编辑:其他变量似乎也存在多个问题。所以我对这个问题做了一点概括:我怎样才能根据
要按其分组的列
要按+ASC或DESC排序的列
在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();
如果没有树,那将是伟大的 我现在详细阐述了这个问题的解决方案: 解决方案与以前一样使用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;
如果它使用变量而不是硬编码的值,但结果不同,则听起来像是匹配问题。结果有什么不同?你能告诉我这两种情况下的分组标准是什么吗?是否按原始字符串值BRANDNAME分组,而不将其关联为列名?请看下半页的两个例子:我可以;找不到任何证据表明查询理解可以与DynamicGroupBy一起工作-您能显示不起作用的代码吗?也许你没有完成你的想法?请看我的编辑。谢谢