none
linq 2010 RRS feed

  • Question

  • Since I am new to linq, I am asking this question. I would like to know how to setup a linq statement for a C# 2010 application that communicates with a sql server2008 r2 database. I have the following two questions:
     1. I would like to know how to select 6 specific columns from one database table. They key to the table is based on an identity seed that is increment by one.
     2. i would like to know how to pick the largest (maximum) and smallest (minimum) value for a create date in one table called main.

    Friday, June 22, 2012 1:54 PM

Answers

  • Hi scamper_cat;

    I will assume that you have and know how to created you data context which model the database in code, if not please read this blog post Using LINQ to SQL (Part 1).

    To your question, "1. I would like to know how to select 6 specific columns from one database table.", To select any number of columns you enumerate the column names in the select clause of the Linq query. For example lets say you have the Northwind database and you created the DataContext and called it ctx. You want to return three columns from the Employees table. This is how you can structure your query.

    var results = from emp in ctx.Employees
                  Select new {
                      emp.FirstName, 
                      emp.LastName, 
                      emp.City
                  };

    The above query will return three columns from the table, FirstName, LastName and City. If you wanted to return all columns then the query would be as follows:

    var results = from emp in ctx.Employees
                  Select emp;

    To your question, "2. i would like to know how to pick the largest (maximum) and smallest (minimum) value for a create date in one table called main", see code below.

    // In the following code I am assuming a table name main and the date column called CreateDate
    // Retrns max date
    var results = (from maxd in ctx.main
                   select maxd.CreateDate).Max( );
                  
    // Retrns min date
    var results = (from mind in ctx.main
                   select mind.CreateDate).Min( );

      


    Fernando (MCSD)

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

    Friday, June 22, 2012 8:02 PM
  • Please have a look at MSDN ref about LINQ query operations

    http://msdn.microsoft.com/en-us/library/bb397927.aspx

    • Marked as answer by scamper_cat Friday, June 22, 2012 8:00 PM
    Friday, June 22, 2012 3:21 PM

All replies

  • Please have a look at MSDN ref about LINQ query operations

    http://msdn.microsoft.com/en-us/library/bb397927.aspx

    • Marked as answer by scamper_cat Friday, June 22, 2012 8:00 PM
    Friday, June 22, 2012 3:21 PM
  • Hi scamper_cat;

    I will assume that you have and know how to created you data context which model the database in code, if not please read this blog post Using LINQ to SQL (Part 1).

    To your question, "1. I would like to know how to select 6 specific columns from one database table.", To select any number of columns you enumerate the column names in the select clause of the Linq query. For example lets say you have the Northwind database and you created the DataContext and called it ctx. You want to return three columns from the Employees table. This is how you can structure your query.

    var results = from emp in ctx.Employees
                  Select new {
                      emp.FirstName, 
                      emp.LastName, 
                      emp.City
                  };

    The above query will return three columns from the table, FirstName, LastName and City. If you wanted to return all columns then the query would be as follows:

    var results = from emp in ctx.Employees
                  Select emp;

    To your question, "2. i would like to know how to pick the largest (maximum) and smallest (minimum) value for a create date in one table called main", see code below.

    // In the following code I am assuming a table name main and the date column called CreateDate
    // Retrns max date
    var results = (from maxd in ctx.main
                   select maxd.CreateDate).Max( );
                  
    // Retrns min date
    var results = (from mind in ctx.main
                   select mind.CreateDate).Min( );

      


    Fernando (MCSD)

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

    Friday, June 22, 2012 8:02 PM