I'm trying apply query from one collection to another. Sample of my test:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Expression sourceExpression = Amazings().Where(x => x.Name.Equals("Kasa")).AsQueryable().Expression;
Expression<Func<Amazing, bool>> predicate = Expression.Lambda<Func<Amazing, bool>>(sourceExpression, Expression.Parameter(typeof(Amazing)));
var col2 = Amazings().Where(predicate.Compile());
}
private IEnumerable<Amazing> Amazings()
{
var amazings = new List<Amazing>
{
new Amazing
{
Name = "Kasa",
},
new Amazing
{
Name = "Ma@p2a"
}
};
return amazings;
}
class Amazing
{
public string Name { get; set; }
}
}
What is wrong? When I run this test in debug I get the exception:
You can not use expressions such as „System.Linq.EnumerableQuery`1[UnitTestProject1.UnitTest1+Amazing]” for the return type „System.Boolean”.
If I run this without parameter I got exception that I don't have parameters.
If I change parameter to boolean type I got exception: you can't use element bool type for delegate parameter type Amazing...
I tested few more options but nothing works.
Also I found that the key may by casting Expression to: MethodCallExpression but it's not or I can't figurate how.
I tried with somothing like this:
MethodCallExpression e = query.Expression as MethodCallExpression;
MemberExpression memberExpression = (MemberExpression)e.Object;
Expression<Func<Amazing, bool>> getCallerExpression = Expression<Func<Amazing>>.Lambda<Func<Amazing, bool>>(memberExpression);
Unfortunately memberExpression is null.
I searched the web and the only thing I found it's a tip: ~"don't do this".
How can I achieve my goal?
It isn't clear what you want to do... But as Servy wrote, you put the
AsQueryable()in the wrong place. The right place is before the.Where(), and this being a Unit Test, I suggest that the method that returns the data should directly return aIQueryable<T>, to better simulate "normal" queries you can find in code.Try looking if this is what you want...
Note that in the most general case, you can't simply move the
Expression<Func<Amazing, bool>>condition of a.Where(), because you could have a.Where().Select()or something similar.Other possible solution, if you know that the last method is a
.Where()and you really want to "move" theExpression<Func<Amazing, bool>>: