3

So I have this code:

 public ActionResult SearchIndex(string id)
    {
        string searchString = id;


        var members = from m in db.Members
                      select m;

        if (!String.IsNullOrEmpty(searchString))
        {
            members = members.Where(m => m.town.Equals(searchString));
        }

        return View(members);
    }

This selects all the town names in my database and I've got it to display in a drop down menu.

How can I change this so that when it's displaying the town names, it will only show each town name once.

For example, if I have 3 londons in the database it will show all 3 londons in the drop down menu. I want it to only show 1 london

2 Answers 2

3

You want to use the Distinct method on IEnumerable

var towns = members.Where(m => m.town.Equals(searchString))
                  .Select(m => m.town).Distinct();

This will Select only the town value, then get the Distinct values, so London will only come up once.

Sign up to request clarification or add additional context in comments.

4 Comments

@user1300788 Sorry, thats because you are trying to overwrite the members variable with something that isnt the same type. Create a new variable called towns (which is more meaningful anyway) and set that.
I still get the same error :/ Can you edit your answer to show how you do this?
I feel really silly but I posted the wrong bit of my code to where the distinct would go. @foreach (var item in Model) { string mytown = item.town.*enter distinct here* <option value="@mytown"> @mytown</option> how would I fit distinct into this?
It would be something like @foreach (var item in Model.Select(m => m.Town).Distinct()) ... Then you would just use item, instead of mytown.
2

I'm not sure that the accepted answer addresses your original question, which was to return an enumerable of Member from a Controller to a View, with an optional filter on town, restricting the list to a maximum of one member in each town:

var members = db.Members; // Or .AsQueryable();

if (!String.IsNullOrEmpty(searchString))
{
    // Apply the conditional filter, returning Members
    members = members.Where(m => m.town == searchString);
}

// First of each member only (randomly)
return View(members
             .GroupBy(m => m.town)
             .Select(grp => grp.First()));

It seems however that your real requirement is to

  • View with filtered IEnumerable<Member> as the @Model
  • As a secondary requirement, populate a dropdown with distinct towns

In this case, you can also project an IEnumerable<string>, e.g. into ViewBag, or better, create a custom ViewModel class containing both the filtered list of Member and the distinct towns for the drop down.

ViewBag.DistinctTowns = db.Members.Select(m => m.town).Distinct();

2 Comments

How can I add an orderBy alphabetical to this bit(m => m.town).Distinct(); ? Code works great btw thanks
@user1300788 .Distinct().OrderBy(t => t); (and +1 StuartLC)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.