none
linq to sql RRS feed

  • Question

  • Since I am new to working with linq to sql in visual studio 2010, I have the following two questions:

    1. Can you point me to a good source I can use as a reference to learn linq to sql?

    2. Using linq to sql, I would like to know how to:

    a. Find out what the minimum and maximum date values are in the receive date column in a sql server 2008 r2 database. The receive date column  is used as the key to the table.

    B. Once I know what the minimum and maximum receive dates values are, then I want to only allow users to select values that are within those date ranges.

    Friday, November 25, 2011 3:57 AM

Answers

All replies

  • > Using linq to sql, I would like to know how to: a. Find out what the minimum and maximum date values are in the receive date column in a sql server 2008 r2 database. The receive date column is used as the key to the table.

      
    the code below creates a database with the dates table
    and requests min and max date

    using System;
    using System.Data.Entity;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication4
    {
        public partial class Form1 : Form
        {
            public Form1()
            {  
                var d = new DataStore();
                // creates database: WindowsFormsApplication4.DataStore
                if (d.Database.CreateIfNotExists())
                {
                    // for test
                    for (int i = 0; i < 10; i++)
                        d.Dates.Add(new Date { Receive = DateTime.Now.AddYears(i) });
                    d.SaveChanges();
                }
    
                var min = new TextBox { Parent = this, Dock = DockStyle.Top };
                var max = new TextBox { Parent = this, Dock = DockStyle.Top };
    
                this.Menu = new MainMenu();
                this.Menu.MenuItems.Add("Load", delegate
                {
                    min.Text = d.Dates.Min(r => r.Receive).ToString();
                    max.Text = d.Dates.Max(r => r.Receive).ToString();
                });
            }
        }
        
        public class Date
        {
            public int Id { get; set; }
            public DateTime Receive { get; set; }
        }
    
        public class DataStore : DbContext
        {
            public DbSet<Date> Dates { get; set; }
            protected override void OnModelCreating(DbModelBuilder mb)
            {
                base.OnModelCreating(mb);
                mb.Entity<Date>().HasKey(t => t.Id).ToTable("Dates");
            }
        }
    }
    
     

    instead of DbContext you can use DataContext. an example is here.
    Friday, November 25, 2011 4:00 PM
  • Hi midnight_car,

    I think this post to be a question type can help you better, so I change the type for you. Malobukv has replied your seconde question, based on your first question, let me give you some links to learn LINQ to SQL.

    I think these links can help you:

    LINQ to SQL

    Using LINQ to SQL

    LINQ to SQL Tutorial

    If you have any problem, you can post your question in LINQ to SQL forum.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by midnight_car Monday, November 28, 2011 7:49 PM
    Monday, November 28, 2011 8:56 AM
    Moderator