Click here to Skip to main content
15,885,546 members
Articles / LINQ
Tip/Trick

LINQ to SQL – Execute SQL Queries

Rate me:
Please Sign up or sign in to vote.
2.73/5 (4 votes)
16 Oct 2011CPOL 29.4K   9   2
LINQ to SQL – Execute SQL Queries
In “LINQ to SQL”, we can use ExecuteQuery method of DataContext object to Execute SQL queries as follows:

Reference: LINQ to SQL – Execute SQL Queries.

class Program  
    {  
        static void Main(string[] args)  
        {  
            // create the context  
            NorthWindDataContext context = new NorthWindDataContext();  
      
            IEnumerable<Category> catList = context.ExecuteQuery<Category>(  
                "Select c1.CategoryId, c1.CategoryName from Categories as c1");  
      
            foreach (var item in catList)  
            {  
            Console.WriteLine(item.CategoryName);  
            }  
        }  
    }  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
Symphony Services, Banglore, 560 087, India

Comments and Discussions

 
GeneralMy vote of 2 Pin
Rob Grainger1-Jun-14 23:15
Rob Grainger1-Jun-14 23:15 
That's not really an article - just a code snippet. No discussion of how result columns match to object fields.

Looks like a simple copy/paste from MSDN
Generalthanks, for the hard work. I was looking for this solution. Pin
digish77717-Oct-11 19:20
digish77717-Oct-11 19:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.