代码之家  ›  专栏  ›  技术社区  ›  QA_Col

从CSharp中的List获取HashSet和Dictionary,按嵌套属性分组

  •  -1
  • QA_Col  · 技术社区  · 5 年前

    对不起,我是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响应。

    1 回复  |  直到 5 年前
        1
  •  1
  •   TheGeneral    5 年前

    如果我理解这个问题。我猜你只是想 GroupBy ,以及 ToDictionary ,这将产生 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());
    

    获取哈希集

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

    其他资源

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

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

    Enumerable.ToDictionary Method

    创建一个 Dictionary<TKey,TValue> 从a IEnumerable<T> .