我有以下课程:
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;
sections.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://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;
使用Microsoft自己的AlphaKeyGroup类