Ayu Asri Lestari
Ayu Asri Lestari

Reputation: 55

'int' does not contain a definition for 'Contains' and the best extension method overload 'Queryable.Contains

I want to filter the grid view by Id, but something error in my syntax

This my HomeController:

public ActionResult Index(int searching)
{
   return View(db.ex_op.Where( x => x.id_ats.Contains(searching) || searching = null).ToList());
}

this my HomeView:

@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
    @Html.TextBox("searching")
    <input type="submit" value="Search" />
}

this my Error:

'int' does not contain a definition for 'Contains' and the best extension method overload 'Queryable.Contains(IQueryable,int)' requires a receiver of type 'IQueryable' ```

Thank you so much!

Upvotes: 3

Views: 4604

Answers (1)

Nguyễn Văn Phong
Nguyễn Văn Phong

Reputation: 14228

You should change from .Contains() to == to be able to compare int

public ActionResult Index(int searching)
{
   return View(db.ex_op.Where( x =>  searching == null || x.id_ats == searching ).ToList());
}

or You still want to filter by Contains, you can convert .ToString() along with change type of param to string like this

public ActionResult Index(string searching)
{
   return View(db.ex_op.Where( x => searching == null || x.id_ats.ToString().Contains(searching)).ToList());
}

Updated

Basically, Unless you use (int? searching) What means nullable int, the Default value of int is Zero, So you no need to check null.

db.ex_op.Where( x => x.id_ats == searching).ToList()

Upvotes: 4

Related Questions