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

    }
}

2 Answers 2

1
var query = collection;

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

var result = query.GroupBy(c => c.Property).Select(g => g.Key);
Sign up to request clarification or add additional context in comments.

Comments

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);

7 Comments

ok, but if both of the expressions are the properties of the class, eg FirstExpression and SecondExpression?
@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.
I have updated the question, please check if it makes sense now
@petrov.alex: Not really. Please show how FirstExpression and SecondExpression are defined.
has not really done this part, but was thinking to do it in this was: public Expression<Func<IEnumerable<TEntity>, IEnumerable>> FirstExpression { get; set; } public Expression<Func<IEnumerable<TEntity>, IEnumerable>> SecondExpresion { get; set; } possible that it all dont have sense, just started to dig into expressions
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.