Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / LINQ

LINQ to SQL – Execute SQL Queries

2.73/5 (4 votes)
16 Oct 2011CPOL 30K  
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)