65.9K
CodeProject is changing. Read more.
Home

LINQ to SQL – Execute SQL Queries

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.73/5 (4 votes)

Oct 14, 2011

CPOL
viewsIcon

30003

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);  
            }  
        }  
    }