蒙哥的c#骨料

蒙哥的c#骨料,c#,mongodb,C#,Mongodb,我试图使用C#中的聚合从我在mongo中的日志中获取统计数据,但我总是得到错误: 必须将组聚合字段“RecordDate”定义为表达式 在物体内部 这是我的密码,有人能告诉我这是怎么回事吗 IMongoCollection<LogRecord> myCollection = MongoClient.GetDatabase("DatabaseName").GetCollection<LogRecord>("CollectionName"); List<GroupedD

我试图使用C#中的聚合从我在mongo中的日志中获取统计数据,但我总是得到错误:

必须将组聚合字段“RecordDate”定义为表达式 在物体内部

这是我的密码,有人能告诉我这是怎么回事吗

IMongoCollection<LogRecord> myCollection = MongoClient.GetDatabase("DatabaseName").GetCollection<LogRecord>("CollectionName");
List<GroupedData> logSats = myCollection.Aggregate<LogRecord>()
                    .Group<LogRecord, StatsKeys, GroupedData>(
                    t => new StatsKeys
                    {
                        RecordDate= t.RecordDate.ToString("%Y-%m-%d"),
                        Type = t.Type,
                        User = t.UserName
                    },
                    g => new GroupedData
                    {

                        count = g.Count(),
                        Success = g.Count(t => !t.Error),
                        Erros = g.Count(t => t.Error),
                        RecordDate = g.Key.RecordDate,
                        Type = g.Key.Type,
                        User = g.Key.User,
                        AvgTime = g.Average(t => t.FirstStepTime + t.SecondStepTime)
                    }
                    ).ToList();
IMongoCollection myCollection=MongoClient.GetDatabase(“DatabaseName”).GetCollection(“CollectionName”);
List logSats=myCollection.Aggregate()
.组(
t=>newstatskey
{
RecordDate=t.RecordDate.ToString(“%Y-%m-%d”),
类型=t.类型,
User=t.UserName
},
g=>newgroupeddata
{
count=g.count(),
Success=g.Count(t=>!t.Error),
Erros=g.Count(t=>t.Error),
RecordDate=g.Key.RecordDate,
类型=g.Key.Type,
User=g.Key.User,
AvgTime=g.平均值(t=>t.FirstStepTime+t.SecondStepTime)
}
).ToList();

LogRecord中的RecordDate是DateTime,而其他的则是一个字符串。

根据Greg Stanley的建议,我在GroupedData类中添加了一个StatsKeys变量(在本例中称为“Key”),并使用Key=g.Key代替它,结果成功了

 IMongoCollection<LogRecord> myCollection = MongoClient.GetDatabase("DatabaseName").GetCollection<LogRecord>("CollectionName");
    List<GroupedData> logSats = myCollection.Aggregate<LogRecord>()
                        .Group<LogRecord, StatsKeys, GroupedData>(
                        t => new StatsKeys
                        {
                            RecordDate= t.RecordDate.ToString("%Y-%m-%d"),
                            Type = t.Type,
                            User = t.UserName
                        },
                        g => new GroupedData
                        {

                            count = g.Count(),
                            Success = g.Count(t => !t.Error),
                            Erros = g.Count(t => t.Error),
                            Key = g.Key,
                            AvgTime = g.Average(t => t.FirstStepTime + t.SecondStepTime)
                        }
                        ).ToList();
IMongoCollection myCollection=MongoClient.GetDatabase(“DatabaseName”).GetCollection(“CollectionName”);
List logSats=myCollection.Aggregate()
.组(
t=>newstatskey
{
RecordDate=t.RecordDate.ToString(“%Y-%m-%d”),
类型=t.类型,
User=t.UserName
},
g=>newgroupeddata
{
count=g.count(),
Success=g.Count(t=>!t.Error),
Erros=g.Count(t=>t.Error),
键=g键,
AvgTime=g.平均值(t=>t.FirstStepTime+t.SecondStepTime)
}
).ToList();

直接在为我工作的
GroupedData
对象上使用
g.Key
-是否有可能?我在
ToString(“%Y-%m-%d”)
刚返回字符串文字时也遇到了问题,因此可能值得检查它是否按预期运行。我不得不改变方法,使用三个单独的赋值语句,例如
Day=t.RecordDate.Day
,然后我可以将其流到
GroupedData
阶段。