1

with reference to this post, unfortunately I've some problem again.

In reality, not all my entities contains the same common properties, nonetheless I need to inherit/implement my interface, so, in someone cased the properties are declarative only, otherwise for other case.

Here the problem:

public static IQueryable<T> Create<T>(ObjectContext context) 
    where T : class, IEntity
{         
    var query = context.CreateObjectSet<T>().AsQueryable();         
    return query.Where(x => x.CommonProperties == "some value"); // problem here!!    
} 

In fact, if neither of my entities (documents) contain common properties or else any of them, the final query will not browsable and an exception will be generated.

I've tried with following code snippet without success:

System.Reflection.PropertyInfo p = query.ElementType.GetProperty("common1");
if (p != null) query = query.Where(x => x.common1 == "value.."); // problem here!!

In this case the p variable never null, so my query is destinated to failure..

Help me please..

Community
  • 1
  • 1
Bit
  • 11
  • 1
  • 3
  • Oh, you poor guy, who the hell forces you to write common query code for things which have nothing in common? Well, if you really must, take a look at the spectacular answer of Bennor McCarthy in this question: http://stackoverflow.com/questions/4782001/how-to-include-an-and-expression-that-checks-for-a-property-and-its-value It doesn't cover exactly the same problem but a very similar one, so perhaps you need to tweak the heavy reflection code snippets in the answer a bit. But it's a starting point. Good luck! – Slauma Apr 05 '11 at 16:46
  • @Slauma: It started few days ago http://stackoverflow.com/questions/5496713/entity-wrapper-custom ... Still the same issue and still the same headstrongness. – Ladislav Mrnka Apr 05 '11 at 17:38

1 Answers1

1
ParameterExpression itemParameter = Expression.Parameter(typeof(T));
return query.Where(Expresion.Equal(Expression.Property(itemParameter, "COMMONPROP_NAME"), Expression.Constant("VALUE")));
Double Down
  • 898
  • 6
  • 13