English 中文(简体)
如何为dict物中相同的关键收集价值?
原标题:How to make a collection of values for same key in a dictionary object?

我有一个实体。

 public class ContextElements
{
        public string Property { get; set; }

        public string Value { get; set; }
}

现在,我已经把该实体归入下级(这是对网上服务提供的实际投入的模拟)。

var collection = new List<ContextElements>();

collection.Add(new ContextElements { Property = "Culture", Value = "en-US" });

collection.Add(new ContextElements { Property = "Affiliate", Value = "0" });

collection.Add(new ContextElements { Property = "EmailAddress", Value = "[email protected]" });

collection.Add(new ContextElements { Property = "Culture", Value = "fr-FR" });

collection.Add(new ContextElements { Property = "Affiliate", Value = "1" });

collection.Add(new ContextElements { Property = "EmailAddress", Value = "[email protected]" });

现在,我有一句dict语。

Dictionary<string, List<string>> dictStr = new Dictionary<string, List<string>>();

我所期待的产出是,对于这里的“Culture”、“Affiliate”、“Email Address”等每个特定关键(即财产)来说,这些数值将出现在清单收集中。

i.e. 字典的最终产出是以下产出(显然在操作和编程上)

dictStr.Add("Culture", new List<string>() { "en-US", "fr-FR" });

dictStr.Add("Affiliate", new List<string>() { "0","1" });

dictStr.Add("EmailAddress", new List<string>() { "[email protected]", "[email protected]" 
});

所需帮助

Thanks

最佳回答

我认为,乔宾斯解决办法将奏效,但对于这个小问题而言,“E”无数不可兑换成名单,你是你的理论家的第二个一般性论点。 相反:

collection.GroupBy(x => x.Property).ToDictionary(x => x.Key, x => x.Select(y => y.Value).ToList());
问题回答
var dictStr = new Dictionary<string, List<string>>();
foreach(var element in collection)
{
    List<string> values;
    if(!dictStr.TryGetValue(element.Property, out values))
    {
        values = new List<string>();
        dictStr.Add(element.Property, values);
    }
    values.Add(element.Value);
}

If you can use LINQ (.NET 3.5 and above I think) you can do:

Dictionary<string, List<string>> dictStr = collection.GroupBy(x => x.Property)
          .ToDictionary(x => x.Key, x => x.Select(y => y.Value).ToList());




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签