0

我有以下课程:

public class Grouping<TKey, TElement> : IGrouping<TKey, TElement>
{
    private readonly IGrouping<TKey, TElement> _group;
    public Grouping(IGrouping<TKey, TElement> group)
    {
        this._group = group;
    }
    public TKey Key { get { return _group.Key; } }
    public IEnumerator<TElement> GetEnumerator()
    {
        return _group.GetEnumerator();
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return _group.GetEnumerator();
    }
}

public class Sections
{
    public IEnumerable<Organisation> Organisations { get; set; }
}

以下代码使用它(organisationRepository 返回一个 IEnumerable):

        organisationRepository = new OrganisationRepository();
        var tempOrganisations = await organisationRepository.GetAll();
        var groupedOrganisations = from currentOrg in tempOrganisations
                                   group currentOrg by currentOrg.name into grouped
                                   select new Grouping<string, Organisation>(grouped);

        sections.Organisations = groupedOrganisations; 
        Organisations = null;

section.Organisations 是一个IEnumerable<Organisation>,所以我收到以下错误:

Error   1   Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<*.ViewModel.Grouping<string,*.Models.*.Models.Entities.Organisation>>' to 'System.Collections.Generic.IEnumerable<*.Models.*.Models.Entities.Organisation>'. An explicit conversion exists (are you missing a cast?) C:\Users\James\Documents\Visual Studio 2012\Projects\*\*\ViewModel\DashboardViewModel.cs    65  38  *

任何想法我如何将分组转换为 IEnumerable

来源:

http://blog.falafel.com/Blogs/NoelRice/noel-rice/2011/08/12/Categorized_Lists_with_Windows_Phone_7_LongListSelector

http://dotnet.dzone.com/articles/using-longlistselector-control

更好的来源:(现在我找到了,我会试一试)

http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj244365(v=vs.105).aspx

非常感谢:https ://www.youtube.com/watch?v=zlfNsw_JM6U

编辑://

我现在回家了,尝试了很多不同的东西,转换为 IEnumerable 仍然存在问题

+       $exception  {System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[*.ViewModel.AlphaKeyGroup`1[*.Models.*.Models.Entities.Organisation]]' to type 'System.Collections.Generic.IEnumerable`1[*.Models.*.Models.Entities.Organisation]'.
   at *.ViewModel.DashboardViewModel.<LoadOrganisationSection>d__2.MoveNext()}  System.Exception {System.InvalidCastException}

这是我目前正在做的事情:

var tempOrganisations = await organisationRepository.GetAll();
            List<AlphaKeyGroup<Organisation>> groupedOrganisations = AlphaKeyGroup<Organisation>.CreateGroups(tempOrganisations,
                System.Threading.Thread.CurrentThread.CurrentUICulture,
                (Organisation o) => { return o.name; }, true);


            sections.Organisations = (IEnumerable<Organisation>)groupedOrganisations; 

使用微软自己的 AlphaKeyGroup 类

4

0 回答 0