English 中文(简体)
利用林克从一个类别到另一个类别
原标题:Convert IGrouping from one class to another using linq

我有两门小组和运动员小组。

public class PlayerTeams
{
    public int ID { get; set; }
    public string PlayerName { get; set; }
    public string PlayerCountry { get; set; }
    public bool IsActive { get; set; }

    public string PlayerTeam { get; set; }
}

public class Players
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
    public bool? Status { get; set; }
}

页: 1 运动员名称/编码,按<代码>PlayerTeam加以分类。

var groupedPlayers = teams
            .OrderBy(x => x.PlayerName)
            .GroupBy( x => x.PlayerTeam)
            .ToList();

其类型如下:<代码>List<IGrouping<string,sexualTeams>>,但我希望该代码为List<Igrouping<string, Hawaiis>>,因为我不想在每一行中提供多余的关键信息。

我如何能够做到这一点? 我只能想像<条码>。 我也未能做到这一点。

是否有这样做的可信方法?

最佳回答

如果你能够改变分类,我只想:

var groupedPlayers = teams
            .OrderBy(x => x.PlayerName)
            .GroupBy(x => x.PlayerTeam, Players.FromPlayerTeam)
            .ToList();

<代码>Players.FromPlayerTeam为上的固定方法。 运动员,采用<代码>PlayerTeam,并填写<编码>。

Additionally, I d suggest using ToLookup instead of GroupBy - a Lookup is exactly what you want here, without bothering with the ToList call.

问题回答

www.un.org/Depts/DGACM/index_spanish.htm 这并不是说,只是一个想法。

如果你在转换你的林克声明方面遇到困难,预计这一发言将<代码>。 Igrouping, to a string list, it may have to select/strong>.

var groupedPlayers = new List<string>();
groupedPlayers = teams
        .OrderBy(x => x.PlayerName)
        .GroupBy(x => x.PlayerTeam, Players.FromPlayerTeam)
        .Select(x => x.Key) // << added select
        .ToList();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...