PaulBinCT2
PaulBinCT2

Reputation: 221

Adding OR to my LINQ query?

So I have the "Ever Expanding LINQ Query"... I found myself having to tack on a number of parameters and I'm getting crosseyed. I have this:

 var ud = db.Updates
   .Where(c => c.Sold.Equals(false)&&(c.Status.Equals(("Pending"))))
   .GroupBy(c => c.Vehicle)
   .Select(x => x
      .OrderByDescending(y => y.TimeStamp)
      .First())
   .ToList();

And I need to add an OR to the "c.Status.Equals" to test for multiple string values... such as "Pending" || "Rejected" what's the right way/place to do this?

Thanks very much for any assistance!

Upvotes: 1

Views: 132

Answers (3)

Rion Williams
Rion Williams

Reputation: 76557

You might want to consider storing an array of values to see if any of the constraints are met :

// Store all of the statuses you need to match within a collection
var validStatuses = new []{ "Pending", "Rejected", ... };

Then you would just need to update your Where() clause to check if your existing status matched any of those within your previously defined collection :

db.Updates.Where(c => !c.Sold && validStatuses.Contains(c.Status)))

Upvotes: 11

Jammore
Jammore

Reputation: 11

Try to use || or operator

   (c.Status.Equals(("Pending")|| c.Status.Equals("Rejected"));

Upvotes: 0

Lucas Limão
Lucas Limão

Reputation: 105

(c.Status.Equals("Pending") || c.Status.Equals("Rejected"))

var ud = db.Updates

         .Where(c => c.Sold.Equals(false) &&(c.Status.Equals("Pending") || c.Status.Equals("Rejected")))
         .GroupBy(c => c.Vehicle)
        .Select(x => x.OrderByDescending(y => y.TimeStamp).First()).ToList();

Upvotes: 0

Related Questions