How exactly does the problem manifest? Since the C# expression compiler (used in the LINQ above) uses the memberinfo tokens directly (rather than relying on strings as reflection), I can't see that you could do any better. Likewise, assuming it is an IL obfuscator (not a source obfuscator), re-writing it as a query expression should achieve nothing, nix, zip, zero and nada. You you can try...
var first = (from f in [whatever]
where f.FileName == fileName
orderby f.Position descending
select f).FirstOrDefault();
What exactly happens?
Edit based on comment: if the problem is the "capture", you could try manually building the expression with a constant (instead of a captured value) - where Foo
is your type:
var param = Expression.Parameter(typeof(Foo), "f");
var body = Expression.Equal(Expression.PropertyOrField(param, "FileName"),
Expression.Constant(filename));
var predicate = Expression.Lambda<Func<Foo, bool>>(body, param);
then use:
.Where(predicate).OrderByDescending(f => f.Position).FirstOrDefault();
The problem, of course, is convincing it that "FileName" and Foo.FileName
must stay the same...
And here's a version that doesn't need the string:
Expression<Func<Foo, string>> liftFileName = foo => foo.FileName;
var predicate = Expression.Lambda<Func<Foo, bool>>(
Expression.Equal(liftFileName.Body, Expression.Constant(filename)),
liftFileName.Parameters);