PaulBinCT2 PaulBinCT2 - 1 year ago 31
C# Question

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)

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!

Answer Source

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)))