Assuming you are talking about a directory, then you would have your letters action an event or set a variable that you can use in your linq.

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

Maybe when they click on 'C' it does ?letter=C in querystring

You can then pick it out and use it as part of my above query, simplified slightly (Look up general usage of EF to create context etc)


This method will return a snippet of html markup with an A-Z roulette of links in it. If there are no jobs under the current letter then it will just show the letter without a link. I think you can modify this to your MVC project quite easily.

private string GetAlphaRouletteLinksHtmlMarkup()
    var JobList = new List<String>() {
        "American Chemical Society",
        "America's Job Bank",
        "Art Careers",
        "Asian MBA",
        "Back Door Jobs",
        "Career Search",
        "Careers 2005",
        "City of Evansville jobs",
        "College Recruiter",
        "Conservation Job Board",
        "Cuyahoga County Public Library",
        "Dentist Jobs Help",
        "Evansville Courier & Press Classifieds",
        "Federal Government Jobs",
        "Indiana Health Careers",
        "Indiana Job Central",
        "Indiana Youth Institute",
        "Inside Jobs",
        "Jackson County Industrial Development Corporation",
        "Job Application Center",
        "Job Search USA",
        "Job Seeker's Bookmarks",
        "Jobs in Indianapolis Web site",
        "Journal of Young Investors",
        "Learn More Indiana",
        "Louisville, KY jobs",
        "News-Line Communications",
        "On-Line Career Center",
        "Online Recruiters Directory",
        "Quintessential Careers",
        "Riley Guide",
        "SallieMae True Careers",
        "Simply Hired",
        "State government job opportunities",
        "Student Employment Opportunities",
        "Teacher Jobs Help",
        "The Ladders Career Advice",
        "U. S. Department of State",
        "Work In Sports",

    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);
            // no html
            outputHtml.Append(" | ");

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

All thats left for you to do is merge this code in and also keep your existing attempt which outputs the list of individual jobs.

Update - I improved by moving more of it into a single LINQ statement by pushing the first letters into a sub-select. Code posted is the updated version without the massive JobList declaration:

Update 2 - I replaced the string split with some more LINQ which generates the uppercase alphabet in this snippet and merged the updates together.

Related Articles