Citizen SP
Citizen SP

Reputation: 1411

like operator gives zero results

I created a actionresult in ASP MVC which should show records matching with my Linq query. It was working correctly until I added:

records.State == '0' &&

This query (in t-sql) returns the correct results in SQL management studio, but not with Linq:

   public ActionResult Index(string q)
    { 
               viewModel.Records = from records in db.records
                                   where records.State == '0' &&
                                   records.Title.Contains(q)
                                   || records.Title.Contains(q)
                                   ||records.Project.Contains(q)
                                   || records.Owner.Contains(q)
                                   || records.Number.Contains(q)
                                   select events;
    }

Upvotes: 1

Views: 54

Answers (4)

Shyju
Shyju

Reputation: 218922

Your code will work if the type of State property is of char type because '0' is a char

If State is of type int/tinyint(byte), you should do this

where records.State == 0 
&& records.Title.Contains(q)

Upvotes: 2

Ian
Ian

Reputation: 30823

Be careful on how you put your logic. If what you want is

records.State == '0' && [whatever previous logic you have]

You should put all your previous logic in parentheses:

viewModel.Records = from records in db.records
                   where records.State == '0' &&
                   (records.Title.Contains(q)
                   || records.Project.Contains(q)
                   || records.Owner.Contains(q)
                   || records.Number.Contains(q))
                   select events;

Also, note that you have duplicate conditions records.Title.Contains(q), I remove it in my solution.

In addition, if your records.State is of numeric type (like int, not char/string), then you should not use apostrophe, but should use the number directly:

viewModel.Records = from records in db.records
                   where records.State == 0 &&
                   (records.Title.Contains(q)
                   || records.Project.Contains(q)
                   || records.Owner.Contains(q)
                   || records.Number.Contains(q))
                   select events;

Upvotes: 5

Rion Williams
Rion Williams

Reputation: 76597

Your Contains() calls look correct (although you have Title.Contains() twice) but you mention that your State property is a tinyint, so you will want to compare it as such (instead of a string as you previously were):

public ActionResult Index(string q)
{ 
           viewModel.Records = from records in db.records
                               where records.State == 0 && (records.Title.Contains(q)
                               ||records.Project.Contains(q)
                               || records.Owner.Contains(q)
                               || records.Number.Contains(q))
                               select events;
}

Upvotes: 2

veena panakanapalli
veena panakanapalli

Reputation: 461

Please try below

public ActionResult Index(string q)
{ 
           viewModel.Records = from records in db.records
                               where records.State == 0 &&
                              ( records.Title.Contains(q)
                               || records.Title.Contains(q)
                               ||records.Project.Contains(q)
                               || records.Owner.Contains(q)
                               || records.Number.Contains(q))
                               select events;
}

Upvotes: 2

Related Questions