如何将给定数据转换为IEnumerable对象 [英] How to convert given data into IEnumerable object

查看:37
本文介绍了如何将给定数据转换为IEnumerable对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#4中有下面的代码,在这里我试图使用linq进行排序和分组.

I have below code in c# 4, where I am trying to use linq for ordering, grouping.

IList<Component> components = Component.OrganizationalItem.OrganizationalItem.Components(true);
IEnumerable<Component> baggage = components.Where(x => x.IsBasedOnSchema(Constants.Schemas.BaggageAllowance.ToString()))
                                           .OrderBy(x => x.ComponentValue("name").StringValue("Code")) 
                                           .GroupBy(x => x.ComponentValue("name").StringValue("Code")); 

在上面的示例中,当我尝试使用 GroupBy 时出现错误,请参见以下内容:

In above sample when I am trying to use GroupBy it is giving error, please see below:

无法将类型' System.Collections.Generic.IEnumerable< System.Linq.IGrouping< string,Tridion.ContentManager.ContentManagement.Component>> '隐式转换为' System.Collections.Generic.IEnumerable< Tridion.ContentManager.ContentManagement.Component> ".存在显式转换(您是否缺少演员表?)*

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Linq.IGrouping<string,Tridion.ContentManager.ContentManagement.Component>>' to 'System.Collections.Generic.IEnumerable<Tridion.ContentManager.ContentManagement.Component>'. An explicit conversion exists (are you missing a cast?)*

推荐答案

GroupBy 的结果将是

The result of GroupBy will be an IGrouping<string, Component> - it's a sequence of groups of components, rather than one sequence of components. That's the whole point of grouping. So this should be fine:

IEnumerable<IGrouping<string, Component>> baggage = ... query as before ...;

或者只使用隐式输入:

var baggage = ...;

然后您可以遍历各组:

foreach (var group in baggage)
{
    Console.WriteLine("Key: {0}", group.Key);
    foreach (var component in group)
    {
        ...
    }
}

这篇关于如何将给定数据转换为IEnumerable对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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