On Apr 9, 8:07*pm, NvrBst <nvr...@gmail.com> wrote:
> The "total.Union(current);" was silly on my part, I knew I was suppose
> to assign *I should of looked at it closer*
*The last queue I made
> also had another mistake I think... *Constantly calling the where
> clause makes it "AND" together while I want it to "OR" together, so I
> think only the ".Any(...)" will give me a hope of doing what I want.
> That or maybe the "A || B || C" == !(!A ^ !B ^ !C)" thing I was
> attempting in my 2nd post.
Actually, you're right, my last suggestion with chaining Where() does
AND, not OR. For OR, you have to use Union().
A problem there is that I'm not sure how well Union() will be
optimized. Maybe LINQ to SQL is smart enough to generate a single SQL
SELECT with multiple OR'd conditions in WHERE. Maybe it will actually
generate UNION ALL, but then SQL Server will optimize the query
correctly. And maybe it will be less efficient (but then again, maybe
the data set is small enough that you don't care).
Anyway, another option that you always have is the most general one -
use classes Expression and Expression<T> to manually build the
expression tree as needed, which you can then feed to Enumerable.Where
(). E.g.:
string[] testValues = { "T", "B", "F" };
var rowParam = Expression.Parameter(typeof(myTable), "row");
// Generate a sequence of expression trees, one for each value from
testValues,
// corresponding to "row.VALUE.StartsWith(value)"
var tests =
from value in testValues
select
Expression.Call(
Expression.Property(rowParam, "VALUE"),
"StartsWith",
new[] { typeof(string) },
new[] { Expression.Constant(value) });
// Combine expression trees generated on previous stage into a
single one with operator || (short-circuiting "or" aka OrElse)
var body = tests.Aggregate((t1, t2) => Expression.OrElse(t1, t2));
// Build the complete lambda expression from body and parameters it
uses
var condition = Expression.Lambda(body, rowParam)
// Use the lambda
var query = myDB.myTables.Where(condition);