C#从List获取HashSet和Dictionary,按嵌套属性分组

Obtaining HashSet and Dictionary, grouping by nested property, from List in C#

对不起,我是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; }
}

在同一个命名空间中 classes:

class 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; }
}

class 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; }
}

和 class 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(或从对象 List 的嵌套 属性 中获取 HashSet)。

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);

现在,我想按 ConversationId 将日志分组到字典中。

在 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 响应。

如果我理解问题。我猜你只是想要 GroupByToDictionary,这将产生 Dictionary<string,List<Log>>

给定

var list = new List<Log>(){...};

用法

var dict = list
   .GroupBy(x => x.Response.Context.ConversationId)
   .ToDictionary(x => x.Key, x => x.ToList());

获取HashSet

var hashSet = list
   .Select(x => x.Response.Context.ConversationId)
   .ToHashSet();

其他资源

GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)

Groups the elements of a sequence according to a specified key selector function.

Enumerable.ToDictionary Method

Creates a Dictionary<TKey,TValue> from an IEnumerable<T>.