Is there a way to dynamically decide what objects are selected by a query?
For example, I have the following query:
public ContractInstance GetContract( int contractID )
var query = ( from contract in context.ContractOutput
from result in context.CalculatorResult
from data in context.UserInput
where contract.ID == contractID
where result.ID == contract.CalculatorResult_ID
where data.ID == result.UserInput_ID
Contract = contract,
Result = result,
Data = data,
Portfolio = data.Portfolio,
OldContract = contract.OldContract,
CreditInfo = contract.CreditInfo,
//...20 more objects related to data/contract/result are selected
My select statement is pretty big because I'm trying to restrict it to one trip to the database.
However, at runtime, I can form a list of actual objects that will be used in that session. So what I want to do is use that list to modify my query's select statement.
Since those are the only objects that will be used, then it will be better to load only those objects (right?)
Is that possible?
If not, is there another/better way to achieve my goal?
Edited byoyenFriday, July 01, 2011 7:36 AMadded the actual question
Sorry, but no... You can't design it as a LINQ query and get what you want.
However, you may take a look at Entity SQL and using ObjectQuery.Execute functions. You can then create a dynamic SELECT statement, and then use the create an ObjectQuery<T> to perform your query. When it has executed, you may cast the DbDataReader
from ObjectQuery.ExecuteReader to your ContractInstance class.