dotnetcore / dotnetcore/sharding-core

使用多级嵌套匿名类型接收多级GroupBy结果时,会抛出Member Not Found的异常

Open
#244 5 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C#
Stars
1.3k
Forks
183
Avg merge
7h 43m
Merged PRs (30d)
1

Description

假设现在有以下实体:
``` csharp
public class TestEntity
{
public Id { get; set; }
public DateTime Time { get; set; }
public int Type { get; set; }
}
```
分表路由使用按月分表。

此时如果我有以下查询(注意:这里该查询触发了跨表查询逻辑)
```csharp
var begin = DateTimeOffset.UtcNow.Date.AddDays(-40).DateTime;
await dbContext.Set()
.Where(x=>x.Time >= begin)
.GroupBy(x => x.Time)
.Select(x => new
{
Time = x.Key,
Groups = x.GroupBy(s => s.Type)
.Select(s =>
{
Type = s.Key,
Count = s.Count()
})
})
.ToListAsync();
```
该查询会导致`ShardingCore.Extensions.StringFieldOrderExtension.GenerateSelector`方法中,抛出如下异常
![image](https://github.com/dotnetcore/sharding-core/assets/49139854/e59c5d1b-f748-483a-b497-926e765c9f88)
原因似乎是匿名类型在Visit表达式的时候将`Groups`和`Type`作为`selectGroupKeyProperties`传入了`OrderWithExpression`,但是未在顶层类型中找到该字段。

而当我声明一个Class来接收Select结果时,该查询可以正常执行,根据代码来看
![image](https://github.com/dotnetcore/sharding-core/assets/49139854/bd632f76-fae7-44cb-8a24-f84bbbab6363)
匿名类型接收结果走了`NewExpression`逻辑
声明一个Class接收结果走了`MemberInitExpression`逻辑

该问题导致的结果是使用多级嵌套匿名类型接收多级`GroupBy`查询结果时,查询无法正常工作

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.