xoxo_tw
xoxo_tw

Reputation: 269

Linq search query using ||

I get this to work ok:

var results = db.GetProducts().Where(p => p.ProductName.Equals(searchQuery, StringComparison.OrdinalIgnoreCase));

When I use the || comparision to get my new query as:

var results = db.GetProducts().Where(p => p.ProductName.Equals(searchQuery, StringComparison.OrdinalIgnoreCase)) || db.GetProducts().Where(p => p.ProductId.ToString().Equals(searchQuery, StringComparison.OrdinalIgnoreCase));

I end up with:

    Error   1   Operator '||' cannot be applied to operands of type 
'System.Collections.Generic.IEnumerable<Uppgift_1.Models.MyProduct>' and 
'System.Collections.Generic.IEnumerable<Uppgift_1.Models.MyProduct>'    C:\Home\Programming
\cSharp\projects\MVC_projects\Uppgift_1\Uppgift_1\Controllers\ProductController.cs
    23  27  Uppgift_1

How can I do if I want to include both statements in the search query ?

Upvotes: 0

Views: 522

Answers (2)

juhan_h
juhan_h

Reputation: 4011

Try this:

var results = db.GetProducts()
    .Where(p => p.ProductName.Equals(searchQuery, StringComparison.OrdinalIgnoreCase)
    || p.ProductId.ToString().Equals(searchQuery, StringComparison.OrdinalIgnoreCase));

Upvotes: 1

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174289

You need to put both conditions in the same Where.

var results = db.GetProducts()
                .Where(p => p.ProductName
                             .Equals(searchQuery, 
                                     StringComparison.OrdinalIgnoreCase) ||
                            p.ProductId.ToString()
                             .Equals(searchQuery,
                                     StringComparison.OrdinalIgnoreCase));

Upvotes: 1

Related Questions