在Asp.net中使用Linq创建A - Z索引的最佳实践

时间:2013-08-16 16:26:35

标签: c# jquery asp.net asp.net-mvc linq

我想创建一个A-Z索引,它通过linq从SQL数据库中提取信息,并想知道在最顶层创建非常接近的东西时最好的方法是什么?乙..... | Z和用户点击它填充的字母。

到目前为止我所拥有的是:

@{ 
  var career = new career_servicesEntities(); 
  var result = (from item in career.JobLists orderby item.Name ascending select item); 

  foreach (var res in result) 
  { 
      <div> <ul> <li><a href="@res.Link">@res.Name</a></li> </ul> </div>
  }
 } 

我正在尝试重新创建的内容类似于此页:

2 个答案:

答案 0 :(得分:0)

假设您正在谈论一个目录,那么您可以让您的字母操作一个事件或设置一个可以在您的linq中使用的变量。

var myData = entity.Where(o=>o.colName.StartsWith(letter)).OrderBy(o=>o.colName);

也许当他们点击'C'时它会吗?letter = C in querystring

然后您可以选择它并将其用作上述查询的一部分,稍微简化(查看EF的一般用法以创建上下文等)

答案 1 :(得分:0)

此方法将返回一个带有链接的A-Z轮盘的html标记片段。如果当前信件下没有工作,那么它只会显示没有链接的字母。我想你可以很容易地将它修改为你的MVC项目。

private string GetAlphaRouletteLinksHtmlMarkup()
{
    var JobList = new List<String>() {
        "AccessibleEmployment",
        "accessIndiana",
        "AccountingNet",
        "Alldiversity.com",
        "American Chemical Society",
        "America's Job Bank",
        "Art Careers",
        "Asian MBA",
        "Back Door Jobs",
        "Career Search",
        "Careerbliss",
        "Careerbuilder",
        "CareerJet",
        "CareerMagazine",
        "CareerMosaic",
        "CareerOverview",
        "Careers 2005",
        "City of Evansville jobs",
        "College Recruiter",
        "CollegeGrad.com",
        "ComputerJobs",
        "Conservation Job Board",
        "Cuyahoga County Public Library",
        "Dentist Jobs Help",
        "EMPLOYMENTCROSSING",
        "Evansville Courier & Press Classifieds",
        "Exam2Jobs",
        "Experience.com",
        "Federal Government Jobs",
        "Flipdog",
        "FPSelectJobs",
        "hotjobs",
        "Hound",
        "Idealist",
        "Indeed",
        "Indiana Health Careers",
        "Indiana Job Central",
        "Indiana Youth Institute",
        "IndianaCAREERConnect",
        "Inside Jobs",
        "Internships",
        "Jackson County Industrial Development Corporation",
        "Jamminjobs",
        "Job Application Center",
        "Job Search USA",
        "Job Seeker's Bookmarks",
        "Job-Applications",
        "JobCo",
        "Jobs in Indianapolis Web site",
        "JobScribble",
        "Journal of Young Investors",
        "Learn More Indiana",
        "Louisville, KY jobs",
        "Mediapost",
        "NBMBAA",
        "News-Line Communications",
        "On-Line Career Center",
        "Online Recruiters Directory",
        "PeaceCorps",
        "Quintessential Careers",
        "Riley Guide",
        "RiseSmart",
        "SallieMae True Careers",
        "ScienceJobs.com",
        "Simply Hired",
        "SmartBrief",
        "Snagajob.com",
        "State government job opportunities",
        "Student Employment Opportunities",
        "Teacher Jobs Help",
        "The Ladders Career Advice",
        "U. S. Department of State",
        "USAJOBS",
        "wetfeet",
        "WomensJobSearch",
        "Work In Sports",
        "WorkplaceDiversity"
    };

    var letters = from alpha in Enumerable.Range('A', 'Z' - 'A' + 1).Select(c => c.ToString())
                  let firstLetters = (from job in JobList
                                      orderby job
                                      group job by job[0].ToString().ToUpper() into firstLetters
                                      select firstLetters.Key)
                  select new
                  {
                      Letter = alpha,
                      IsLink = firstLetters.Contains(alpha)
                  };

    StringBuilder outputHtml = new StringBuilder();
    foreach (var item in letters)
    {
        if (item.IsLink)
        {
            // add html
            outputHtml.AppendFormat("<a href=\"#roulette{0}\">{1}</a> | ", item.Letter, item.Letter);
        }
        else
        {
            // no html
            outputHtml.Append(item.Letter);
            outputHtml.Append(" | ");
        }
    }

    return outputHtml.ToString().TrimEnd(" | ".ToCharArray());
}

您需要做的就是合并此代码并保留现有的尝试,输出单个作业列表。

更新 - 通过将第一个字母推入子选择,将更多内容移动到单个LINQ语句中,我进行了改进。发布的代码是没有大量JobList声明的更新版本:

更新2 - 我用更多LINQ替换了字符串split,它在这个片段中生成了大写字母,并将更新合并在一起。