C# 如何创建字母列表?

C# 如何创建字母列表?,c#,asp.net-mvc,algorithm,logic,C#,Asp.net Mvc,Algorithm,Logic,大家好,我正在尝试实现按列排列的alpha顺序列表 如图所示 但是我的算法不清楚,也许有人可以在内部帮助我 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

大家好,我正在尝试实现按列排列的alpha顺序列表

如图所示

但是我的算法不清楚,也许有人可以在内部帮助我

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);
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=(来自EntityBase.db.Services中的se
orderby se.Name
选择se.ToList();
int total=fullServices.Count;
var grouped=(以字母形式从l开始)
选择新服务信息
{
字母=l,
Services=EntityBase.db.Services.Where(se=>se.Name.StartsWith(l)).ToList(),
Total=EntityBase.db.Services.Where(se=>se.Name.StartsWith(l)).Count()
}).ToList();
字典结果=新字典();
changecell:
列表项=新列表();
而(ji?1:0))*i))
{
添加(新的ServiceInfo(){Letter=letters[j],Services=\u Services});
结果.增加(i)项;
如果(i==6)
转到出口;
i++;
后藤细胞;
}
//启动渲染服务
if(fullServices.ElementAt.Name.StartsWith(字母[j]))
{
_添加(fullServices.ElementAt);
s++;//列表中的增量服务
}
else//字母开关
{
添加(新的ServiceInfo(){Letter=letters[j],Services=\u Services});
j++;
去换字母;
}
}//终端渲染服务
}
出口:
返回视图(结果);
在我的代码中,我看到遗漏的字母X Y ZÅÄÖ 看起来就像

下面是呈现字典的代码

<% 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>
  <%} %>


请帮助……

好吧,代码不是特别清楚,这一点您当然是对的:)

我并不真正遵循代码的主循环,但这里有一个更简单的起点。请注意,它不会正确地将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个相等的列,尽管…

这可能有点离谱-我刚刚在记事本中完成了这项工作(目前无法访问IDE)。看起来您正在尝试填充
字典结果
实例,我假设您的
视图
方法了解如何在列中布局结果

下面是:

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);
}
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=newdictionary();
foreach(字母中的var字母)
{
var services=(来自EntityBase.db.services中的se
其中se.Name.StartsWith(字母)
orderby se.Name
选择选择新服务信息
{
字母,
Services=EntityBase.db.Services.Where(se=>se.Name.StartsWith(字母)).ToList(),
Total=EntityBase.db.Services.Where(se=>se.Name.StartsWith(字母)).Count()
}).ToList();
结果.增加(i,服务);
}

如果它是正确的,它可能不会是最快的实现,但它更具可读性。

是的,我已经更新了帖子并包含了属于render的代码,但仍然存在按列创建分隔的问题。我更接近Jon Skeet发布的代码,但这没有帮助。最后,我做了所有这些goto只是为了分隔。如何创建下面的信件和服务列表我知道。。。