我正在研究从 Gremlin 查询数据。经过几天的努力,我现在可以使用
ToDynamic
查询数据并返回预期的输出格式。但是,我需要返回一个强类型的类以保持代码整洁。但是,当使用强类型类时,我无法访问 outE
来获取 inV
进行进一步查询。
这是产生所需输出的代码:
IVertexGremlinQuery<Conversation> conversationQuery = gremlinQuerySource
.V<Account>()
.Where(account => account.Id == accountId)
.OutE<Has>()
.InV<Conversation>();
long count = await conversationQuery.Count().FirstAsync();
var paginatedConversations = conversationQuery
.Skip(pageIndex * pageSize)
.Limit(pageSize);
var conversationWithMessages = paginatedConversations
.Project(p => p.ToDynamic()
.By(conversation => conversation.Id)
.By(conversation => conversation.Title)
.By(conversation => conversation.IsDirectMessage)
.By("Participants", conversation => conversation.OutE<BelongsTo>().InV<Account>()
.Project(account => account.ToDynamic()
.By(account => account.Id)
.By(account => account.Email)
.By(account => account.FullName)
.By(account => account.Gender)
).Fold())
.By("Messages", p => p.OutE<Has>().InV<ConversationMessage>()
.Project(p => p.ToDynamic()
.By(p => p.Id)
.By(p => p.Content)
.By(p => p.SenderId)
.By(p => p.Timestamp)
).Fold())
);
var source = await conversationWithMessages.ToArrayAsync();
return source;
它会产生以下输出:
[
{
"Id": "58d7f798-ec17-48ff-a0b4-61d2e429d4e0",
"Title": "",
"IsDirectMessage": true,
"Participants": [
{
"Id": "46c8bdb6-be60-4eb1-b03f-96be2ebb4f00",
"Email": "[email protected]",
"FullName": "",
"Gender": "0"
},
{
"Id": "c4085232-15e4-4555-864a-a441a546d098",
"Email": "[email protected]",
"FullName": "",
"Gender": "0"
}
],
"Messages": [
{
"Id": "83ecd9e7-6820-4b0f-a0ef-89cf0c4d55e1",
"Content": "helllllllllllllllllllllo",
"SenderId": "46c8bdb6-be60-4eb1-b03f-96be2ebb4f00",
"Timestamp": "1726437703827"
},
{
"Id": "e71e7269-2ec7-4579-a0bb-289a904c627b",
"Content": "jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj",
"SenderId": "46c8bdb6-be60-4eb1-b03f-96be2ebb4f00",
"Timestamp": "1726437799277"
}
]
}
]
但是当我更改查询以使用强类型类时,我失去了对
outE
方法的访问权限。
IVertexGremlinQuery<ConversationDto> conversationWithMessages = paginatedConversations
.Project(p => p.To<ConversationDto>()
.By(conversation => conversation.Id, c => c.Values(a => a.Id))
.By(conversation => conversation.Title, c => c.Values(a => a.Title))
.By(conversation => conversation.IsDirectMessage, c => c.Values(a => a.IsDirectMessage))
// CS1061 'ConversationDto' does not contain a definition for 'OutE' and no accessible extension method 'OutE' accepting a first argument of type 'ConversationDto' could be found(are you missing a using directive or an assembly reference ?)
.By(conversation => conversation.OutE<BelongsTo>().InV<Account>()
.Project(account => account.To<AccountDto>()
.By(account => account.Id, a => a.Values(a => a.Id))
.By(account => account.Email, a => a.Values(a => a.Email))
.By(account => account.FullName, a => a.Values(a => a.FullName))
.By(account => account.Gender, a => a.Values(a => a.Gender))
).Fold(), (c, participants) => c.Participants = participants)
// CS1061 'ConversationDto' does not contain a definition for 'OutE' and no accessible extension method 'OutE' accepting a first argument of type 'ConversationDto' could be found(are you missing a using directive or an assembly reference ?)
.By(conversation => conversation.OutE<Has>().InV<ConversationMessage>()
.Project(message => message.To<ConversationMessageDto>()
.By(message => message.Id, m => m.Values(m => m.Id))
.By(message => message.Content, m => m.Values(m => m.Content))
.By(message => message.SenderId, m => m.Values(m => m.SenderId))
.By(message => message.Timestamp, m => m.Values(m => m.Timestamp))
).Fold(), (c, messages) => c.Messages = messages)
);
没有关于这个问题的文档,所以我不知道去哪里查找。然而,这似乎是寻找专家来帮助解决此问题的最佳场所。
从不同顶点查询数据时仍然无法返回强类型类。但是,在检索数据后,我知道它与我的目标类的格式匹配。因此,我使用 Newtonsoft.Json 将数据序列化为字符串,然后反序列化以获得我想要的对象,因为 System.Text.Json 在我的情况下效果不佳。这是完整的代码:
public async Task<ApiResult<ConversationDto>> GetInboxAsync(string accountId, int pageIndex, int pageSize)
{
IVertexGremlinQuery<Conversation> conversationQuery = gremlinQuerySource
.V<Account>()
.Where(account => account.Id == accountId)
.OutE<Has>()
.InV<Conversation>();
long count = await conversationQuery.Count().FirstAsync();
IVertexGremlinQuery<Conversation> paginatedConversations = conversationQuery
.Skip(pageIndex * pageSize)
.Limit(pageSize);
IGremlinQuery<dynamic> conversationWithMessages = paginatedConversations
.Project(p => p.ToDynamic()
.By(conversation => conversation.Id)
.By(conversation => conversation.Title)
.By(conversation => conversation.IsDirectMessage)
.By("Participants", conversation => conversation.OutE<BelongsTo>().InV<Account>()
.Project(account => account.ToDynamic()
.By(account => account.Id)
.By(account => account.Email)
.By(account => account.FullName)
.By(account => account.Gender)
).Fold())
.By("Messages", p => p.OutE<Has>().InV<ConversationMessage>()
.Project(p => p.ToDynamic()
.By(p => p.Id)
.By(p => p.Content)
.By(p => p.SenderId)
.By("Sender", p => p.OutE<BelongsTo>().InV<Account>()
.Project(account => account.ToDynamic()
.By(account => account.Id)
.By(account => account.Email)
.By(account => account.FullName)
.By(account => account.Gender)
))
.By(p => p.Timestamp)
).Fold())
);
dynamic[] source = await conversationWithMessages.ToArrayAsync();
string jsonString = JsonConvert.SerializeObject(source);
ConversationDto[] conversations = JsonConvert.DeserializeObject<ConversationDto[]>(jsonString)!;
ApiResult<ConversationDto> apiResult = ApiResult<ConversationDto>.CreateAsync(
conversations,
count,
pageIndex,
pageSize
);
return apiResult;
}