0

I have queries that are built like this:

public static List<MyObjectModel> GetData (int MyParam) { using (DataModel MyModelDC = new DataModel()) { var MyQuery = from.... select MyObjectModel { ...} } return new List<MyObjectModel> (MyQuery) } } 

It seems that using compiled linq-to-sql queries are about as fast as stored procedures and so the goal is to convert these queries into compiled queries. What's the syntax for this?

Thanks.

1 Answer 1

1

Put something like this inside of your DataContext (or in your case your "DataModel"):

private static Func<DataModel, int, MyObjectModel> _getObjectModelById = CompiledQuery.Compile<DataModel, int, MyObjectModel>( (dataModel, myParam) => dataModel.PersonDtos.where(c => c.ObjectModelId == myParam).FirstOrDefault() ); 

Then add amethod in there to call it like this:

internal List<MyObjectModel> GetObjectModel(int myParam) { var results = _getObjectModelById(this, myParam); return results.SingleOrDefault(); } 

Inside of your repository where your original method was call the internal function to get the result you are looking for.

Hope this helps -> I can post more code if necessary :)

Sign up to request clarification or add additional context in comments.

3 Comments

All my queries are in different namespaces. Where would the top section of your code go?
The top section goes inside of your linq2sql class that represents your datacontext (i think you're calling it "ObjectModel"). the class that actually represents your sql database in C# code.
sorry, i think you're calling it "DataModel"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.