Best practice for creating an AZ index using Linq in Asp.net

I wanted to create an AZ index that retrieves information from an SQL database via linq and wonders what would be the best approach to creating something very close where you are at the top A | B ..... | Z, and when the user clicks on the letter, it is filled.

What I still have:

@{ 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 looks like this page:

+4
source share
2 answers

Assuming you are talking about a directory, then you will have your actions with letters or an event, or 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' does that mean? letter = C in querystring

Then you can select it and use it as part of my above request, simplify it a bit (look at the general use of EF to create context, etc.)

0
source

This method will return a fragment of html markup with a roulette of AZ links in it. If there are no tasks in the current letter, it will simply show the letter without a link. I think you can easily change this into your MVC project.

 private string GetAlphaRouletteLinksHtmlMarkup() { var JobList = new List<String>() { "AccessibleEmployment", "accessIndiana", "AccountingNet", "Alldiversity.com", "American Chemical Society", "America 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 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", "US 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()); } 

All you have to do is combine this code and save your existing attempt, which displays a list of individual tasks.

Update - I improved by moving it to a single LINQ statement by clicking the first letters in a subsample. The output code is an updated version without a massive JobList :

Update 2 - I replaced the split line with another LINQ, which generates an uppercase letter in this snippet and merges the updates together.

0
source

Source: https://habr.com/ru/post/1497470/


All Articles