Sql – Linq2Sql – Storing Linq Expressions in cleartext (linq) for future dynamic execution

linqlinq-to-sql

I recently posted this:

Linq2Sql – Storing Complex Linq Queries for future dynamic execuction – raw text – possible?

It answered one question, but sent me down a different path because of the subQuery needing to reference the same table the original query already exists in.

I was able to do exactly what I wanted to do in Linqpad using the "let" keyword.

from posMain in DataEventView
let posSub = from posSub1 in DataEventView where posSub1.CheckNumber == posMain.CheckNumber && posSub1.EventTypeID == 7 select posSub1
where posMain.EventTypeID == 6 && posSub.Any()
select posMain

That is effectively creating the sub-query I have been attempting to do.

Now, I was thinking — I would really like to dynamically create the IQueryable object in memory, and then my application can have reference to it, and continue to add expressions to it as needed.

I am thinking I am going to have to do this with the CodeDom, but there may be a more elegant way of getting the IQueryable from just having Raw Text to start with. Has anyone had to store a linq statement for future execution? These statements are dynamic per customer, so they cant just be complied into the framework. Maybe there is a better idea?

Thanks in advance.

Travis