4

I wanted to created an A - Z index that pulls information from a SQL database via linq and was wondering what would be the best approach in regards to creating something very close where you have at the top A | B.....| Z and as a user clicks on a letter it is populated.

What I have so far is:

@{ 
  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>
  }
 } 

What I'm trying to recreate is something like this page:

4

2 回答 2

0

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

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

也许当他们点击“C”时,它会在查询字符串中执行 ?letter=C

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

于 2013-08-16T16:33:22.377 回答
0

此方法将返回一个带有 AZ 轮盘链接的 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 替换了字符串拆分,该 LINQ 在此代码段中生成大写字母并将更新合并在一起。

于 2013-08-16T22:54:36.077 回答