Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In order to avoid using generics (since the types are not known at design time) you could use some reflection and build the expression "by hand"</p> <p>You would need to do this by defining a "Contains" expression inside one Where clause:</p> <pre><code>public IQueryable GetItemsFromContainsClause(Type type, IEnumerable&lt;string&gt; items) { IUnitOfWork session = new SandstoneDbContext(); var method = this.GetType().GetMethod("ContainsExpression"); method = method.MakeGenericMethod(new[] { type }); var lambda = method.Invoke(null, new object[] { "Codigo", items }); var dbset = (session as DbContext).Set(type); var originalExpression = dbset.AsQueryable().Expression; var parameter = Expression.Parameter(type, ""); var callWhere = Expression.Call(typeof(Queryable), "Where", new[] { type }, originalExpression, (Expression)lambda); return dbset.AsQueryable().Provider.CreateQuery(callWhere); } public static Expression&lt;Func&lt;T, bool&gt;&gt; ContainsExpression&lt;T&gt;(string propertyName, IEnumerable&lt;string&gt; values) { var parameterExp = Expression.Parameter(typeof(T), ""); var propertyExp = Expression.Property(parameterExp, propertyName); var someValue = Expression.Constant(values, typeof(IEnumerable&lt;string&gt;)); var containsMethodExp = Expression.Call(typeof(Enumerable), "Contains", new[] { typeof(string) }, someValue, propertyExp); return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(containsMethodExp, parameterExp); } </code></pre> <p>In this case "Codigo" is hard-coded, but it could be a parameter to get any property of the type you define.</p> <p>You could test it by using:</p> <pre><code>public void LambdaConversionBasicWithEmissor() { var cust= new Customer(); var items = new List&lt;string&gt;() { "PETR", "VALE" }; var type = cust.GetType(); // Here you have your results from the database var result = GetItemsFromContainsClause(type, items); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload