从Linq语句中获取字典输出

时间:2022-11-28 14:07:17

This is my entity collection.

这是我的实体集合。

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

When I receive value in EntityCollection, I want to filter it.

当我在EntityCollection中收到值时,我想过滤它。

var filtered = from entity in EntityCollection
                       where entity.Key == entityId
                       select entity.Value;

Now I want only entity.value. So I created a variable:

现在我只想要entity.value。所以我创建了一个变量:

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

How can I cast filtered to entityDetails?

如何将过滤转换为entityDetails?

I tried with filtered.ToList<Dictionary<string, string>>, but no success.

我尝试使用filtered.ToList >,但没有成功。

1 个解决方案

#1


2  

You can get directly from Dictionary instead of using LINQ like yours:

您可以直接从Dictionary获取而不是使用像您这样的LINQ:

List<Dictionary<string, string>> entityDetails = EntityCollection[entityId];

Or if you want to avoid exception from the first approach if dictionary does not have a key

或者,如果您想要避免第一种方法的异常,如果字典没有密钥

List<Dictionary<string, string>> entityDetails;
if (EntityCollection.TryGetValue(entityId, out entityDetails))
{
}

#1


2  

You can get directly from Dictionary instead of using LINQ like yours:

您可以直接从Dictionary获取而不是使用像您这样的LINQ:

List<Dictionary<string, string>> entityDetails = EntityCollection[entityId];

Or if you want to avoid exception from the first approach if dictionary does not have a key

或者,如果您想要避免第一种方法的异常,如果字典没有密钥

List<Dictionary<string, string>> entityDetails;
if (EntityCollection.TryGetValue(entityId, out entityDetails))
{
}