Define selected objects in query at runtime/dynamically


  • 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
    	  select new ContractInstance
    		   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
    	return query.FirstOrDefault()

    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 by oyen Friday, July 01, 2011 7:36 AM added the actual question
    Friday, July 01, 2011 7:17 AM