对不起,我是C#新手,我来自Java 8。
我在用C#检查代码,
在命名空间中
IBM.Watson.Assistant.v1.Model
public class LogCollection
{
public LogCollection();
public List<Log> Logs { get; set; }
public LogPagination Pagination { get; set; }
}
在同一命名空间中,类:
班
Log
public class Log
{
public Log();
public MessageRequest Request { get; set; }
public MessageResponse Response { get; set; }
public string LogId { get; set; }
public string RequestTimestamp { get; set; }
public string ResponseTimestamp { get; set; }
public string WorkspaceId { get; set; }
public string Language { get; set; }
}
班
MessageResponse
public class MessageResponse
{
public MessageResponse();
public MessageInput Input { get; set; }
public List<RuntimeIntent> Intents { get; set; }
public List<RuntimeEntity> Entities { get; set; }
public bool? AlternateIntents { get; set; }
public Context Context { get; set; }
public OutputData Output { get; set; }
public virtual List<DialogNodeAction> Actions { get; }
}
和类
Context
public class Context : DynamicModel<object>
{
public Context();
public string ConversationId { get; set; }
public Dictionary<string, object> System { get; set; }
public MessageContextMetadata Metadata { get; set; }
}
现在我有了
List<IBM.Watson.Assistant.v1.Model.Log> listLog = logCollection.Logs;
我想弄清楚
ConversationId
进入HashSet(或获取
HashSet
从对象的嵌套属性
List
).
In
Java 8
我会使用:
Set<String> setConversationId = listLog.stream()
.map(log -> log.getResponse().getContext().getConversationId())
.collect(Collectors.toSet());
在C#中,我可以做这样的事情吗?
HashSet<string> setConversationId = new HashSet<string>(listLog.HOW_TO_GET_ConversationId_FROM_Context_FROM_Response);
现在,我想按以下方式对日志进行分组
对话ID
在字典里。
Java 8是这样的:
public Map<String, List<Log>> getMapFromList(List<Log> listLog)
{
Map<String, List<Log>> map = listLog
.stream().collect(Collectors.groupingBy(
log -> log.getResponse().getContext().getConversationId(),
Collectors.mapping(log -> log, Collectors.toList())
));
return map;
}
在C#
public Dictionary<string, List<Log>> getDictionaryFromList(List<Log> listLog)
{
Dictionary<string, List<Log>> dictionary = listLog
.???????;
return dictionary;
}
如何根据某个列表的嵌套属性将数据分组到字典中?
注:
:我更喜欢functional/lambda而不是Linq响应。