2009-05-30 101 views
2

大家好我想實現阿爾法下令列如何用字母創建按字母順序排列的列表?

名單上picture

如圖但是我的算法是不明確的,也許有人可以幫助我內

string[] letters = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", 
    "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", 
    "Y", "Z", "Å", "Ä", "Ö", "0-9" }; 

int j = 0, s = 0, i = 1; 
var fullServices = (from se in EntityBase.db.Services 
       orderby se.Name 
       select se).ToList(); 
int total = fullServices.Count; 
var grouped = (from l in letters 
      select new ServiceInfo 
      { 
       Letter = l, 
       Services = EntityBase.db.Services.Where(se => se.Name.StartsWith(l)).ToList(), 
       Total = EntityBase.db.Services.Where(se => se.Name.StartsWith(l)).Count() 
      }).ToList(); 
Dictionary<int, List<ServiceInfo>> result = new Dictionary<int, List<ServiceInfo>>(); 

changecell: 

List<ServiceInfo> item = new List<ServiceInfo>(); 
while (j < letters.Count()) 
{ 
letterchange: 
    List<Service> _services = new List<Service>(); 
    while (s < total) 
    { 
     if ((s == (5 + (total % 5 > i ? 1 : 0)) * i)) 
     { 
     item.Add(new ServiceInfo() { Letter = letters[j], Services = _services }); 
     result.Add(i, item); 
     if (i == 6) 
      goto exit; 
     i++; 
     goto changecell; 
     } 
     //start render services 
     if (fullServices.ElementAt(s).Name.StartsWith(letters[j])) 
     { 
     _services.Add(fullServices.ElementAt(s)); 
     s++;//increment service in list 
     } 
     else //letter switch 
     { 
     item.Add(new ServiceInfo() { Letter = letters[j], Services = _services }); 
     j++; 
     goto letterchange; 
     } 
    }//end render services 

} 
exit: 
return View(result); 

在結果我代碼我看到錯過的字母XYZÅÄÖ ,它看起來像this

這裏是代碼,呈現字典

<% foreach (KeyValuePair<int, List<BL.Models.Infos.ServiceInfo>> col in Model) 
{ %> 
    <ul class="col"> 
    <% foreach (var item in col.Value) 
{ %> 
    <% if (!item.Services.Any()) 
{%> 
    <li class="disabled"> 
     <h1> 
     <%= item.Letter %></h1> 
    </li> 
    <%} 
else 
{ %> 
    <li> 
     <h1> 
     <a href="/service/info/<%= item.Letter %>"><%= item.Letter %></a> 
     </h1> 
    </li> 
    <% foreach (var service in item.Services) 
{ %> 
    <li><a href="/service/info/<%= service.Name %>"><%= service.Name %></a></li> 
    <%} 
} 
}%> 
    </ul> 
    <%} %> 

請幫助...

回答

4

嗯,你肯定是正確的,代碼不是特別清楚:)

我真的不按照你的代碼的主循環,但這裏的一個更簡單的起點。請注意,它不會正確分組0-9(目前只處理0):說實話,我不確定最佳方法。你可能希望把他們關閉,直到你得到不匹配任何正常的字母一些條目...

using System; 
using System.Linq; 

class Test 
{ 
    static void Main(string[] args) 
    { 
     ShowGroups(); 
    } 

    private static readonly char[] Letters = 
     "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ0".ToCharArray(); 

    // This is taking the place of EntityBase.db.Services 
    // for the purposes of the test program 
    public static string[] Services = { "Blogger", "Delicious", 
      "Digg", "Ebay", "Facebook", "Feed", "Flickr", 
      "Friendfeed", "Friendster", "Furl", "Google", 
      "Gosupermodel", "Lastfm", "Linkedin", "Livejournal", 
      "Magnolia", "Mixx", "Myspace", "NetOnBuy", "Netvibes", 
      "Newsvine", "Picasa", "Pownce", "Reddit", "Stumbleupon", 
      "Technorati", "Twitter", "Vimeo", "Webshots", 
      "Wordpress" }; 

    public static void ShowGroups() 
    { 
     var groupedByLetter = 
      from letter in Letters 
      join service in Services on letter equals service[0] into grouped 
      select new { Letter = letter, Services = grouped }; 

     // Demo of how to access the groups 
     foreach (var entry in groupedByLetter) 
     { 
      Console.WriteLine("=== {0} ===", entry.Letter); 
      foreach (var service in entry.Services) 
      { 
       Console.WriteLine (" {0}", service); 
      } 
      Console.WriteLine(); 
     } 
    } 
} 

我不知道你是怎麼打算,雖然拆分結果納入5個等於列。 ..

0

這可能是離開了 - 我剛剛在記事本中完成了這項工作(現在無法訪問IDE)。它看起來像你試圖填充Dictionary<int, List<ServiceInfo>> result實例,我假設你的View方法理解如何在列中佈局結果。

這裏所說:

string[] letters = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 
            "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", 
            "U", "V", "W", "X", "Y", "Z", "Å", "Ä", "Ö", "0-9" }; 

var result = new Dictionary<int, List<ServiceInfo>>(); 
foreach (var letter in letters) 
{ 
    var services = (from se in EntityBase.db.Services 
        where se.Name.StartsWith(letter) 
        orderby se.Name 
        select select new ServiceInfo 
        { 
         Letter = letter, 
         Services = EntityBase.db.Services.Where(se => se.Name.StartsWith(letter)).ToList(), 
         Total = EntityBase.db.Services.Where(se => se.Name.StartsWith(letter)).Count() 
        }).ToList(); 
    result.Add(i, services); 
} 

如果這是正確的,它可能不會是最快的實現,但它更具有可讀性。

+0

是的,我已更新帖子,幷包含屬於渲染的代碼 但是仍然有問題可以通過列創建分隔我更接近* Jon Skeet *發佈的代碼,它在最終 中沒有幫助我做了所有這些* goto's *只是爲了分離。我知道如何用字母和服務創建列表... – omoto 2009-05-30 10:14:41