我可以为此 Linq 分组使用匿名类型吗?

发布于 2024-10-06 08:28:49 字数 920 浏览 2 评论 0 原文

我有以下代码,它生成一个包含多个列表的字典;每个列表都可以使用数字键检索。

public class myClass
{
    public string Group { get; set; }
    public int GroupIndex { get; set; }
    ...
}

public List<MyClass> myList { get; set; }

private Class IndexedGroup
{
    public int Index { get; set; }
    public IEnumerable<MyClass> Children { get; set; }
}

public Dictionary<int, IEnumerable<MyClass>> GetIndexedGroups(string group)
{
    return myList.Where(a => a.Group == group)
                 .GroupBy(n => n.GroupIndex)
                 .Select(g => new IndexedGroup { Index = g.Key, Children = g })
                 .ToDictionary(key => key.Index, value => value.Children);
}

有什么方法可以消除 IndexedGroup 类吗?

我尝试过在 Select 方法中使用匿名类型,如下所示:

.Select(g => new { Index = g.Key, Children = g })

但我得到类型转换错误。

I have the following code which produces a dictionary containing multiple lists; each list can be retrieved with a numeric key.

public class myClass
{
    public string Group { get; set; }
    public int GroupIndex { get; set; }
    ...
}

public List<MyClass> myList { get; set; }

private Class IndexedGroup
{
    public int Index { get; set; }
    public IEnumerable<MyClass> Children { get; set; }
}

public Dictionary<int, IEnumerable<MyClass>> GetIndexedGroups(string group)
{
    return myList.Where(a => a.Group == group)
                 .GroupBy(n => n.GroupIndex)
                 .Select(g => new IndexedGroup { Index = g.Key, Children = g })
                 .ToDictionary(key => key.Index, value => value.Children);
}

Is there any way to eliminate the IndexedGroup class?

I've tried using an anonymous type in the Select method, like this:

.Select(g => new { Index = g.Key, Children = g })

but I get a type conversion error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缪败 2024-10-13 08:28:49

ChildrenIGrouping 转换为 IEnumerable,或显式将通用参数传递给 ToDictionary 调用。

g 参数是一个 IGrouping,它实现 IEnumerable
隐式泛型调用最终会创建一个 DictionaryIGrouping>,它无法转换为 Dictionary IEnumerable>

IndexedGroup 类可以避免这种情况,因为其 Children 属性显式键入为 IEnumerable

例如:

return myList.Where(a => a.Group == group)
             .GroupBy(n => n.GroupIndex)
             .ToDictionary<int, IEnumerable<MyClass>>(g => g.Key, g => g);


另外,您可能对 ILookup接口

Cast Children from IGrouping<T> to IEnumerable<T>, or explicitly pass generic parameters to the ToDictionary call.

The g parameter is an IGrouping<T>, which implements IEnumerable<T>.
The implicit generic calls end up creating a Dictionary<int, IGrouping<MyClass>>, which cannot be converted to a Dictionary<int, IEnumerable<MyClass>>.

This is avoided by your IndexedGroup class, since its Children property explicitly typed as IEnumerable<MyClass>.

For example:

return myList.Where(a => a.Group == group)
             .GroupBy(n => n.GroupIndex)
             .ToDictionary<int, IEnumerable<MyClass>>(g => g.Key, g => g);


Also, you may be interested in the ILookup<TKey, TElement> interface.

樱花落人离去 2024-10-13 08:28:49

您可以完全摆脱 Select() 并调用 .AsEnumerable()

return myList.Where(a => a.Group == group)
             .GroupBy(n => n.GroupIndex)
             .ToDictionary(g => g.Key, g => g.AsEnumerable());

或者您可以将返回类型更改为 ILookup,这基本上就是您想要的数据结构:

public ILookup<int, MyClass> GetIndexedGroups(string group)
{
    return myList.Where(a => a.Group == group)
                .ToLookup(n => n.GroupIndex);                    
}

You could just get rid of the Select() entirely and call .AsEnumerable():

return myList.Where(a => a.Group == group)
             .GroupBy(n => n.GroupIndex)
             .ToDictionary(g => g.Key, g => g.AsEnumerable());

Or you could change your return type to an ILookup, which is basically the data structure you're going for:

public ILookup<int, MyClass> GetIndexedGroups(string group)
{
    return myList.Where(a => a.Group == group)
                .ToLookup(n => n.GroupIndex);                    
}
平定天下 2024-10-13 08:28:49

下面的怎么样?

return myList.Where(a => a.Group == group)
             .GroupBy(n => n.GroupIndex)
             .ToDictionary(g => g.Key, g => g as IEnumerable<MyClass>);

How about the following?

return myList.Where(a => a.Group == group)
             .GroupBy(n => n.GroupIndex)
             .ToDictionary(g => g.Key, g => g as IEnumerable<MyClass>);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文