2016-08-08 6 views
-3
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() }; 

In dem obigen LINQ Code vorsteht, meine Frage ist, wie ich MarketviewModel projizieren, wie Sie sehen Stationsname eine Liste von SubItem ist.Linq zu einer Klasse

Station = g.tolist()

(wie dieses Stück, das ich neu schreiben?), So dass es eine Liste von subitem erstellt Objekte

EDIT

activestations Einheit enthält

  • stationsname (string),
  • stationgroupInt (int),
  • Markt (string)

market stationName StationgroupInt 
---------------------------------- 

A  B    1 
A  C    1 
A  D    1 
+5

Sie projizieren bereits auf diesen Typ. – Servy

+0

Was ist der Typ in 'activeStations'? Sie müssen einige weitere Informationen bereitstellen. – Nkosi

Antwort

0

Sie Select auf Gruppe g verwenden können, um den Unterpunkt zu erstellen.

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() 
      }; 
+0

Danke das funktioniert – user1005310

+0

@ user1005310 Wenn die Antwort nützlich ist, können Sie auch abstimmen. – Nkosi