C#Linq或Lambda获得Dictionary< string,List< string>>从课堂上 [英] C# Linq or Lambda to get Dictionary<string, List<string>> from a class

查看:1171
本文介绍了C#Linq或Lambda获得Dictionary< string,List< string>>从课堂上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到我正在寻找的答案。希望有人可以帮助。



我有一个类,其中包含一些进程的设置信息。每个类都有一个processId,taskId和我当前逻辑不需要的各种其他信息。

  public class ProcessSetting 
{
public int ProcessId {get;组; }
public int TaskId {get;组; }

//其他属性不需要
}

是可以存在的多个ProcessSettings。我将数据拉入列表。可以将processId与多个TaskIds相关联。例如:

  ProcessId:1,TaskId:1 
ProcessId:1,TaskId:1
ProcessId :1,TaskId:2
ProcessId:1,TaskId:3
ProcessId:2,TaskId:3
ProcessId:2,TaskId:4
ProcessId:3,TaskId:1

我最初使用linq从现有的枚举中收集我需要的值:(使用不同最后避免拉入多个记录集ProcessId 1和TaskId 1)

  var baseSettings =(从processSettings中设置
选择新
{
ProcessStr =((ProcessEnum)setting.ProcessId).ToString(),
TaskStr =((TaskEnum)setting.TaskId).ToString()
})。Distinct();

现在给我一个列表,只有processId和taskId。我在这里发现了一些逻辑,导致我走向正确的方向,但不完全是我需要的。这是什么:

 字典< string,List< string> = baseSettings.GroupBy(x => x.ProcessStr).ToDictionary(x => x.Key,x => x.ToList()); 

但是,这是不正确的。我收到一个错误:


无法转换源类型
System.Collections.Generic.Dictionary< ; string,System.Generic.List {ProcessStr:string,
TaskStr:string}>>
到目标类型
System.Collections.Generic .Dictionary< string,System.Collections.Generic.List< string>>


想要一个值为{ProcessStr,TaskStr}的Key,任何人都可以指向正确的方向?

解决方案

x.ToList 您必须首先选择匿名类型的字符串属性:

  Dictionary< string,List< string>> procTasks = baseSettings 
.GroupBy(x => x.ProcessStr)
.ToDictionary(g => g.Key,g => g.elect(x => x.TaskStr).ToList());


I cannot seem to find the answer I am looking for. Hopefully someone here can help out.

I have a class that contains setting information for some processes. Each class has a processId, taskId, and various other pieces of information not needed for my current logic.

public class ProcessSetting
{
    public int ProcessId { get; set; }
    public int TaskId { get; set; }

    // Other properties not needed
}

There are multiple ProcessSettings that can exist. I am pulling the data into a List. It is possible to have a processId associated with multiple TaskIds. For example:

ProcessId: 1, TaskId: 1
ProcessId: 1, TaskId: 1
ProcessId: 1, TaskId: 2
ProcessId: 1, TaskId: 3
ProcessId: 2, TaskId: 3
ProcessId: 2, TaskId: 4
ProcessId: 3, TaskId: 1

I am initially using linq to just gather the values that I need from the existing enum: (Using distinct at the end to avoid pulling in the multiple record set of ProcessId 1 and TaskId 1)

var baseSettings = (from setting in processSettings
                    select new
                              {
                                  ProcessStr = ((ProcessEnum)setting.ProcessId).ToString(),
                                  TaskStr = ((TaskEnum)setting.TaskId).ToString()
                              }).Distinct();

This now gives me a list with just the processId and taskId. I found some logic on here that lead me in the right direction, but isn't exactly what I need. Here is what that was:

Dictionary<string, List<string> = baseSettings.GroupBy(x => x.ProcessStr).ToDictionary(x => x.Key, x => x.ToList());

However, This is incorrect. I am getting an error:

"Cannot convert source type System.Collections.Generic.Dictionary<string,System.Generic.List{ProcessStr:string, TaskStr:string}>> to target type System.Collections.Generic.Dictionary<string,System.Collections.Generic.List<string>>

I don't want a Key with a value of {ProcessStr, TaskStr}. Can anyone point me in the right direction?

解决方案

Instead of x.ToList you have to select the string property of the anonymous type first:

Dictionary<string, List<string>> procTasks = baseSettings
    .GroupBy(x => x.ProcessStr)
    .ToDictionary(g => g.Key, g => g.Select(x => x.TaskStr).ToList());

这篇关于C#Linq或Lambda获得Dictionary&lt; string,List&lt; string&gt;&gt;从课堂上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆