0

I have an xml file which contains query details like

<queries>
  <query>
    <name>GetStudentById</name>
    <statement>select student_name from student where student_id=@id</statement>
   </query>
 </queries>

In C# code, the user will call a method ExecuteQuery(string queryname, hashtable params) I will retrieve the query statement from the xml file using the query name.

The params hash table will contain values like params['@id']=5

I will then use ADO .NET to construct the query and execute it at runtime using ExecuteReader after constructing the query statement and passing it the parameter list.

I want to know if it is possible to do it by using LinQ. Can someone show me how to execute the statement as a LinQ query?

Thanks,

David

2
  • Sounds like a perfect fit for Dapper. Commented Dec 15, 2011 at 10:11
  • That would be a mess and or a lot of work, and you would gain nothing aside frommay be learning LINQ Commented Dec 15, 2011 at 10:17

3 Answers 3

1

Have a look at this SO post. Look at the answer from john skeet where he explains how to use the CSharpCodeProvider to execute the statement at runtime.

You will however have to convert your statement from sql to linq yourself. If there is no particular reason for it having to be linq, I would suggest keeping it in sql because at the end of the day you are going to parse sql to linq and then back to sql.

Even with dynamic linq you will have a problem because you will always have to specify the from class in your query.

Another option is that if you want to use this with your entity framework model, it would be way easier to use entity sql, have a look here and here for more info.

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

Comments

0

It's a considerable leap to go from using string query statements via ADO.NET to Linq to SQL. In the first instance you need to build your Linq to SQL entity model/DataContext. How you go about parsing a sql statement from text to Linq I'm not entirely sure, but if I wanted to look into how to achieve this I would be reading up on expression trees with a view to building a parser that takes your string query and referers to your entity model in order to produce a linq query.

Comments

0

Found something called LinqTextQueryBuilder at codeplex and another MS Library. Links below

LinqTextQueryBuilder

http://msdn.microsoft.com/en-us/vstudio/bb894665.aspx

Comments

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.