C# Linq投影到类

C# Linq投影到类,c#,asp.net-mvc,C#,Asp.net Mvc,您可以使用组g上的选择来创建子项 public class MarketViewModel { public MarketViewModel() { } public string Item { get; set; } public List<SubItem> StationName { get; set; } } public class SubItem { public string Item { get; set; } }

您可以使用组g上的选择来创建子项

public class MarketViewModel
{
    public MarketViewModel()
    {
    }

    public string Item { get; set; }
    public List<SubItem> StationName { get; set; }

}

public class SubItem
{
    public string Item { get; set; }
}

var active = from actives in activeStations
    group actives.StationName by new { actives.Market,actives.StationGroupInt } into g
    select new MarketViewModel { Item = g.Key.Market, StationName = g.tolist() };

您已经在投影到该类型。activeStations中的类型是什么。你需要提供更多的信息。谢谢worked@user1005310若答案有用,你们也可以投赞成票。
var active = from actives in activeStations
             group actives.StationName by new { actives.Market,actives.StationGroupInt } into g
             select new MarketViewModel { 
                 Item = g.Key.Market, 
                 StationName = g.Select(stationName => new SubItem { 
                     Item = stationName 
                 }).Tolist() 
             };