June 2008 - Posts

Working with LINQ To SQL

During these days I am developing a little project with LINQ To SQL for understand this new tecnology. It is a little web application for configure newsletters. Here I report some examples with LINQ To SQL:

Example using JOIN and Anonimous types:

 var objs=from s in dal.Subscribers
                     join ns in dal.SubScribersNewsLetters on s.nlsub_email equals ns.sunl_submail
                     where ns.sunl_nlid==idNL
                     select new{
                         s.nlsub_email,
                         s.nlsub_RagioneSociale,
                         s.nlsub_citta
                     };

 

Example for insert multiple objects in one step:

 List<SubScribersNewsLetter> inserted=new List<SubScribersNewsLetter>();
            foreach(GridViewRow gr in this.gvUtenti.Rows){
                CheckBox cb = (CheckBox)gr.FindControl("cbAssocia");
                if(cb.Checked){
                    string mail=gr.Cells[2].Text;

                    if (this.NotPresent(objs,mail))
                    {
                        SubScribersNewsLetter tmp=new SubScribersNewsLetter();
                        tmp.sunl_nlid=idNL;
                        tmp.sunl_submail=mail;
                        inserted.Add(tmp);
                    }
                }
            }
            dal.SubScribersNewsLetters.InsertAllOnSubmit<SubScribersNewsLetter>(inserted);
            dal.SubmitChanges();

 

Example for get a single record object:

var objs = from o in dal.Subscribers
                       where o.nlsub_email == mail
                       select o;
            if (objs.Count<Subscriber>() > 0)
                return objs.Single<Subscriber>();

Example for IQueryable:

IQueryable<Subscriber> objs = from o in dal.Subscribers
                       join n in dal.SubScribersNewsLetters on o.nlsub_email equals n.sunl_submail
                       where n.sunl_nlid == nl
                       select o;

 

Bye

Antonio

This site

Search

Go

This Blog

Syndication

Sponsors

  • MaximumASP
  • Breaking News
  • Find a Job
  • Social Bookmarking
    Tidebuy Reviews
    Online Shopping
    asp.net hosting
    UK online local dating