Serkan Hekimoglu
Serkan Hekimoglu

Reputation: 4284

LinQ Query Builder to .cs file in C#

As you know we use LinQ Technology to SQL Classes for send and get results to SQL. If I have a table called User in SQL, I write queries in C# like

List<User> allUsers = (from s in dataContext.Users select s).ToList();

or

// Update Query

User u = (from s in dataContext.Users where s.Id.Equals(1) select s).First();

u.Name = "serkan";

dataContext.SubmitChanges();

We called this CRUD services (Create-Update-Delete).

My Question is, if I have a table required CRUD more than 40+, I need to write same codes. It will take too much times to write. Is there any Tool (ex Generator) to get table names from given SQL Con, and write base methods (CRUD) to .cs file? If not, I gonna write a tool in C#, but firstly I want to know. Searched at google a bit, but cant find enough info.

EDIT: While waiting for answers, I started to type a tool. It will be like this, but I will be very happy, If a tool already written in c# :)

private List<string> myClasses = new List<string>();
        private const string myDataContextName = "SurfTurkeyDataContext";

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                StringBuilder sb = new StringBuilder();

                sb.AppendLine("using System;");
                sb.AppendLine("using System.Collections.Generic;");
                sb.AppendLine("using System.Linq;");
                sb.AppendLine("using System.Text;");
                sb.AppendLine(Environment.NewLine);
                sb.AppendLine("namespace LinqToCsGenerator");
                sb.AppendLine("{");
                sb.AppendLine(Environment.NewLine);

                //Class Initilize
                sb.AppendLine("public class Users");
                sb.AppendLine("{");

                foreach (string item in myClasses)
                {
                    sb.AppendLine(Environment.NewLine);

                    // Select All Method
                    sb.AppendLine(String.Format("public List<{0}> GetAll{0}()", item));
                    sb.AppendLine("{");
                    sb.AppendLine(Environment.NewLine);
                    sb.AppendLine(String.Format("{0} dc = new {0}();", myDataContextName));
                    sb.AppendLine(Environment.NewLine);
                    sb.AppendLine(String.Format("List<{0}> all{0} = (from s in dc.{0} select s).ToList();", item));
                    sb.AppendLine(Environment.NewLine);
                    sb.AppendLine(String.Format("return all{0};", item));
                    sb.AppendLine(Environment.NewLine);
                    sb.AppendLine("}");
                    sb.AppendLine(Environment.NewLine);

                    // Insert Method
                    // Delete Method
                    //.
                    //.
                    //.
                    //.
                }

                sb.AppendLine("}");
                sb.AppendLine("}");

                System.IO.File.WriteAllText(@"D:\deneme.cs", sb.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            myClasses.Add("Users");
            myClasses.Add("Products");
        }

Upvotes: 1

Views: 1472

Answers (2)

dexter
dexter

Reputation: 7203

Look at Text Template Transformation Kit for Visual Studio - T4.

Upvotes: 1

Related Questions