2013-02-01 83 views
8

我有一個對象:羣組使用Linq屬性的對象列表?

public class SiteInfo 
     { 
      public string Title { get; set; } 
      public string URL { get; set; } 
      public string Type { get; set; }  

     } 

,我使用創建一個列表: VAR網站=新名單();

 foreach (SPWeb site in web.GetSubwebsForCurrentUser()) 
     { 
      string sitetype = getConfigurationKey(site, "siteType"); 
      //If sites have a site type then add to list 
      if (sitetype != "*ERROR*" && sitetype != "*KEYNOTFOUND*") 
      { 
       SiteInfo s = new SiteInfo(); 
       s.Title = site.Title; 
       s.URL = site.Url; 
       s.Type = sitetype; 

       sites.Add(s); 
      } 
     } 
     //sort list by type 
     sites.Sort((x, y) => string.Compare(x.Type, y.Type)); 

     // serialize and send..  
     JavaScriptSerializer serializer = new JavaScriptSerializer(); 
     StringBuilder sbJsonResults = new StringBuilder(); 
     serializer.Serialize(sites, sbJsonResults); 
etc..... 

但是我想要做的是在序列化之前按類型對網站進行分組。這可能使用LINQ或其他方法。

+1

排序依據,也許 –

+1

我會通過查看所有的LINQ方法的列表,看是否啓動它們中的任何一個看起來都可能與分組功能有關。如果你這樣做了,'GroupBy'應該很適合你。 -1清楚地沒有研究工作。 – Servy

回答

11

這聽起來像你想要的東西,如:

// No need to sort sites first 
var grouped = sites.OrderBy(x => x.Type) 
        .GroupBy(x => x.Type); 

然後,只需序列化grouped。然而,我不知道在JSON中看起來像什麼樣的IGrouping ......並且類型將在每種情況下出現。您可以希望類似:

var grouped = sites.OrderBy(x => x.Type) 
        .GroupBy(x => x.Type) 
        .Select(g => new { Type = g.Key, 
             Sites = g.Select(site => new { 
                  site.Title, 
                  site.URL 
                 } }); 

認爲這會給你一個更好的JSON結構。

2

var sites = new List<SiteInfo>() 
{ 
    new SiteInfo(){Title="1",Type="a",URL="http://aaaa"}, 
    new SiteInfo(){Title="2",Type="b",URL="http://bbbb"}, 
    new SiteInfo(){Title="3",Type="a",URL="http://aaaa"}, 
    new SiteInfo(){Title="4",Type="b",URL="http://bbb"}, 
}; 

var json = new JavaScriptSerializer().Serialize(sites.GroupBy(s => s.Type)); 

將產生

[ 
    [{"Title":"1","URL":"http://aaaa","Type":"a"},{"Title":"3","URL":"http://aaaa","Type":"a"}], 
    [{"Title":"2","URL":"http://bbbb","Type":"b"},{"Title":"4","URL":"http://bbb","Type":"b"}] 
] 

var json = new JavaScriptSerializer().Serialize(sites.GroupBy(s => s.Type) 
                 .ToDictionary(x=>x.Key,x=>x)); 

將產生

{ 
    "a":[{"Title":"1","URL":"http://aaaa","Type":"a"},{"Title":"3","URL":"http://aaaa","Type":"a"}], 
    "b":[{"Title":"2","URL":"http://bbbb","Type":"b"},{"Title":"4","URL":"http://bbb","Type":"b"}] 
} 
1

這裏是一個簡單的控制檯應用程序,你想要做什麼:

static void Main(string[] args) 
{ 
    List<SiteInfo> sites = new List<SiteInfo>() 
    { 
     new SiteInfo() { Title = "Site A", Type = "Whatever 2" }, 
     new SiteInfo() { Title = "Site B", Type = "Whatever 1" }, 
     new SiteInfo() { Title = "Site C", Type = "Whatever 1" }, 
     new SiteInfo() { Title = "Site D", Type = "Whatever 3" }, 
     new SiteInfo() { Title = "Site E", Type = "Whatever 3" } 
    }; 

    var sitesGroupedByType = 
     sites.GroupBy(s => s.Type).Select(g => new { Type = g.Key, 
            Sites = g.Select(site => new 
            { 
              site.Title, 
              site.URL 
            })}); 

    foreach (var siteTypeGroup in sitesGroupedByType.OrderBy(g => g.Type)) 
    { 
     foreach(var site in siteTypeGroup.Sites) 
     { 
      Console.WriteLine(string.Format("Type => {0}, Title => {1}", 
           siteTypeGroup.Type, site.Title)); 
     } 
    } 

    Console.ReadKey(); 
} 

輸出:

Type => Whatever 1, Title => Site B 
Type => Whatever 1, Title => Site C 
Type => Whatever 2, Title => Site A 
Type => Whatever 3, Title => Site D 
Type => Whatever 3, Title => Site E 
相關問題