0

Is it possible to combine dynamically expressions:

from c in collection where c.Property == true select c

with expression

from result in results 
group result by result.Property 
into g 
select new g.Key 

where 'results' should be the collection returned from first expression ?

I am going to use composed expression to fetch the data from db using NHibernate, so I would like the combined expression to be equal to if I would write it as

from c in collection 
where c.Property == true
group c by c.Property
into g
select new g.Key

The expressions are defined in the class:

public class MyClass : MyAbstractClass<User>
{
    public MyClass()
    {
        FirstExpression = users => from user in users where ... select user;              

        SecondExpression = results => from result in results
                       group result by result.Property into g
                       select g.Key

    }
}
petrov.alex
  • 1,089
  • 2
  • 12
  • 20

2 Answers2

1
var query = collection;

if (condition)
    query = query.Where(c => c.Property);

var result = query.GroupBy(c => c.Property).Select(g => g.Key);
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0

Yes, that's possible. Simply assign the result of the first query to the results variable without enumerating it:

var results = from c in collection where c.Property == true select c;

from result in results 
group result by result.Property 
into g 
select g.Key; 

BTW, Your second query is a simple distinct:

results.Distinct(r => r.Property);
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • ok, but if both of the expressions are the properties of the class, eg FirstExpression and SecondExpression? – petrov.alex Jan 11 '13 at 09:39
  • @petrov.alex: Please try to rephrase that sentence, it doesn't make too much sence as it stands right now. Expressions are normally not properties of a class - expressions are written *using* the properties of a class. – Daniel Hilgarth Jan 11 '13 at 09:43
  • I have updated the question, please check if it makes sense now – petrov.alex Jan 11 '13 at 09:54
  • @petrov.alex: Not really. Please show how `FirstExpression` and `SecondExpression` are defined. – Daniel Hilgarth Jan 11 '13 at 09:56
  • has not really done this part, but was thinking to do it in this was: public Expression, IEnumerable>> FirstExpression { get; set; } public Expression, IEnumerable>> SecondExpresion { get; set; } possible that it all dont have sense, just started to dig into expressions – petrov.alex Jan 11 '13 at 10:01
  • In that case it is not possible, because the return type of `FirstExpression` is `IEnumerable` while the parameter of `SecondExpression` is required to be `IEnumerable`. But I don't really see what you are trying to achieve with this anyway. – Daniel Hilgarth Jan 11 '13 at 10:04
  • well, I wanted to separate the filtering part from grouping part as this two parts are supposed to change independently and extract the filtering and grouping expressions to separate classes, and then combine them in some other class and use this combined expression for executing query against DB – petrov.alex Jan 11 '13 at 10:13