繁体   English   中英

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

在同一个命名空间中的类:

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

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 (或从 object 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;
}

如何通过某些列表的嵌套属性将数据分组到字典中?

注意:我更喜欢功能/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> )

根据指定的键选择器 function 对序列的元素进行分组。

Enumerable.ToDictionary 方法

IEnumerable<T>创建Dictionary<TKey,TValue> > 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM