I am new in ASP.NET so I have problem with duplicate records in my search enginee. When I search for the towns, some records are duplicate some of them are tree-douple.
Controller.cs
[Authorize(Roles = "Attorney")]
public ActionResult CreateNext(int? id)
{
var currentUser = manager.FindById(User.Identity.GetUserId());
var nfDoc = db.NFDOCUMENTS.Find(id);
if (nfDoc.UserId == currentUser.Id && nfDoc.FacilityId == null)
{
var contacts = db.Contacts.Where(x => x.ContactCategory.Name == "Facility");
List<string> towns = new List<string>();
foreach (var item in contacts)
{
if (!towns.Contains(item.City))
{
towns.Add(item.City);
}
}
ViewData["towns"] = towns;
var medProviders = db.Contacts.Where(x => x.ContactCategory.Name == "Facility" && x.Firstname != null).ToList();
ViewData["medProviders"] = medProviders;
var pat = db.Patients.Where(x => x.Id == nfDoc.PatientId).FirstOrDefault();
ViewBag.Address = pat.Address1 + ", " + pat.City + ", " + pat.State + ", " + pat.Zip;
ViewBag.InsuranceId = new SelectList(db.Contacts.Where(s => s.ContactCategory.Name == "Insurance Carrier"), "Id", "Firstname");
ViewBag.AdjusterId = new SelectList(db.Contacts.Where(s => s.ContactCategory.Name == "Adjuster"), "Id", "Firstname");
ViewBag.FacilityId = new SelectList(db.Contacts.Where(s => s.ContactCategory.Name == "Facility"), "Id", "Firstname");
ViewBag.DoctorId = new SelectList(db.Contacts.Where(s => s.ContactCategory.Name == "Doctor"), "Id", "Firstname");
ViewBag.PatientId = pat.Id;
ViewBag.PatientName = pat.Firstname + " " + pat.Lastname;
return View();
}
else
{
return RedirectToAction("Create");
}
}
View
SO I expect after searchin engine filted towns, I want to avoid duplicates
<div class="input-group col-md-12">
<input id="search" type="text" class="form-control input-lg" placeholder="Search towns" />
</div>
<ul class="list-group nav nav-pills nav-stacked" style="height: 200px; overflow-x: hidden; overflow-y: auto">
<li><a class="reload-towns"><i class="icon-location4"></i> ALL TOWNS</a></li>
@foreach (var item in towns)
{
<li><a class="town" data-town="@item"><i class="icon-location3"></i> @item</a></li>
}
</ul>
</div>
</div>
</div>


.Select()to select what you want and.Distinct()to select only distinct entries. You don't need to fill lists manually either, just call.ToList()or.ToArray()