none
linq to sql pull more than one row

    Question

  • Since I am new to working  a C# 2010 console application, I have the following question to ask: 

    I would like to use linq to sql on a sql server 2008 r2 database to pull all the data from one column in one particular table. The value for the one array would be varchar(50) There will be more than one row selected at the time. (What I have seen of linq so far is to pull only one value by using singleordefault.)

    Can tell me how to accomplisth this goal and/or point me to a reference that I can use?

     
    Thursday, September 06, 2012 1:42 PM

Answers

  • Hi wendy;

    By default Linq to SQL returns a collection of object. You can have it return just one row / or part of a row by using the SingleOrDefault method. The following is an example of pulling down one column from each row and returning it as a List of strings.

    // Using the Northwind sample database returning the company name from the Customers table.
    var companies = (from c in DataContextInstanse.Customers
                     select c.CompanyName).ToList();

    The variable companies will contain a list of company names only.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, September 06, 2012 2:50 PM
  • Hi Wendy;

    At this link, 101 LINQ SAMPLES, you will find all the Linq methods that can be used. If you click on the method name it will take you to a page showing samples on how to use the method in a query.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, September 06, 2012 2:55 PM

All replies

  • Hi wendy;

    By default Linq to SQL returns a collection of object. You can have it return just one row / or part of a row by using the SingleOrDefault method. The following is an example of pulling down one column from each row and returning it as a List of strings.

    // Using the Northwind sample database returning the company name from the Customers table.
    var companies = (from c in DataContextInstanse.Customers
                     select c.CompanyName).ToList();

    The variable companies will contain a list of company names only.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, September 06, 2012 2:50 PM
  • Hi Wendy;

    At this link, 101 LINQ SAMPLES, you will find all the Linq methods that can be used. If you click on the method name it will take you to a page showing samples on how to use the method in a query.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, September 06, 2012 2:55 PM