Yanayaya
Yanayaya

Reputation: 2184

How do I use an IEnumerable string parameter in my code?

As part of my search page I am allowing the user to make a multi-select on specific search terms using the KendoUI Multiselect widget. These items in the collection are passed to my controller as a parameter. My question is, after I have passed them to my controller how do I use them? more specifically how do I use them in my Where statement which uses the Contains method.

Here is my code for the multi select widget

@(Html.Kendo().MultiSelect()
  .Name("vessel_type")
  .Placeholder("Select Type")
  .BindTo(new List<string>() {
      "AHTS",
      "PSV",
      "Special"
   }))

Here is my controller code which uses the vessel_type as a parameter

public ActionResult Search(IEnumerable<string> vessel_type)
    {
        var vessels = (from o in db.vessels
                       select o);
        vessels = vessels.Where(s => s.vessel_type.Contains(vessel_type));
        return PartialView("_results", vessels);
    }

This line isn't correct because it's expecting a string but I have a collection of mroe than one:

 vessels = vessels.Where(s => s.vessel_type.Contains(vessel_type));

Thanks

Upvotes: 0

Views: 181

Answers (1)

Kapol
Kapol

Reputation: 6463

If I understand the question correctly, I believe you need to perform the check the other way around, that is check if the vessel_type collection contains the vessel's type:

vessels = vessels.Where(s => vessel_type.Contains(s.vessel_type));

Here Contains is an extension method on IEnumerable<T>.

On a side note, since the parameter represents a collection, I think a plural name is more appropriate, for example vessel_types.

Upvotes: 2

Related Questions