LINQ to SQL Cheat Sheet

Reviews
LINQ
to
SQL
in
C# Cheat
Sheet Data context & database create http://damieng.com var db = new MyDataContext(@"server=.\SQLEXPRESS;database=my;integrated security=SSPI"); if (!db.DatabaseExists()) db.CreateDatabase(); Select one var only = db.Customers.SingleOrDefault(c => c.CustID == "CHIPS"); var first = db.Customers.FirstOrDefault(c => c.CustID == "CHIPS"); Insert var customer = new Customer() { CustID = "CHIPS", CompanyName = "Mr. Chips" }; db.Customers.InsertOnSubmit(customer); db.SubmitChanges(); Where, null, contains & type var r = new string[] { "WA", "OR" }; var customers = from c in db.Customers where c is Customer && (c.Region==null || r.Contains(c.Region)) select c; Update customer.ContactName = "Adam"; customer.Location = null; db.SubmitChanges(); Paging & order var page3 = (from c in db.Customers orderby c.ContactName, c.City descending select c).Skip(10).Take(5); Delete db.Customers.DeleteOnSubmit(customer); db.SubmitChanges(); Class/table attributes Join, shape & distinct var labels = (from c in db.Customers join o in db.Orders on c.CustID equals o.CustID select new { name = c.ContactName, address = o.ShipAddress }).Distinct(); [Table(Name="dbo.Customers")] [InheritanceMapping(Code="C", Type=typeof(Customer), IsDefault=true)] [InheritanceMapping(Code="T", Type=typeof(TopCustomer)] Property/column attributes [Column(Storage="_CustomerID", DbType="Int NOT NULL", Name="custid", IsPrimaryKey=true)] [Column(Storage="_Category", DbType="Char(1)", IsDiscriminator=true)] [Column(Storage="_Ver", IsVersion=true AutoSync=AutoSync.Always, UpdateCheck=UpdateCheck.Never, DbType="rowversion NOT NULL", CanBeNull=false, IsDbGenerated=true)] Group, count, sum & projection var totals = from c in db.Customers group c by c.Country into g select new Summary { Country = g.Key, CustomerCount = g.Count(), OrdCount = g.Sum(a=> a.Orders.Count)}; Composite & outer join var r = from c in db.Customers join o in db.Orders on new { c=c.CustID, r=c.Country } equals new { c=o.CustID, r=o.ShipCountry } into j from jo in j.DefaultIfEmpty() select new { c, jo }; Association/relationship attributes [Association(Name="Country_Customer", Storage="_Country", ThisKey="CoID", IsForeignKey=true)] LINQ
to
SQL
in
VB.Net Cheat
Sheet Data context & database create http://damieng.com Dim db = New MyDataContext("server=.\SQLEXPRESS;database=my;integrated security=SSPI") If Not db.DatabaseExists Then db.CreateDatabase() Select one Dim only = db.Customers.SingleOrDefault( Function(c) c.CustID = "CHIPS") Dim first = db.Customers.FirstOrDefault( Function(c) c.CustID = "CHIPS") Insert Dim customer = New Customer() With { .CustID = "CHIPS", .CompanyName = "Mr. Chips" } db.Customers.InsertOnSubmit(customer) db.SubmitChanges() Where, null, contains & type Dim r = New String() {"WA", "OR"} Dim customers = From c In db.Customers _ Where TypeOf c Is Customer AndAlso _ (c.Region Is Nothing OrElse _ r.Contains(c.Region)) Select c Update customer.ContactName = "Adam" customer.Location = Nothing db.SubmitChanges() Paging & order Dim page3 = From c In db.Customers _ Order By c.ContactName, c.City _ Descending Skip 10 Take 5 Select c Delete db.Customers.DeleteOnSubmit(customer) db.SubmitChanges() Class/table attributes Join, shape & distinct Dim labels = From c In db.Customers _ Join o in db.Orders _ On c.CustID Equals o.CustID _ Select name = c.ContactName, _ address = o.ShipAddress _ Distinct _ _ _ Property/column attributes _ _ _ Group, count, sum & projection Dim totals = From c In db.Customers _ Group c By c.Country Into g = Group _ Select New Summary With _ {.Country = Country,.CustomerCount = _ g.Count(),OrdCount = g.Sum(Function(a) _ a.Orders.Count)} Composite & outer join Dim z = From c In db.Customers _ Group Join o In db.Orders On _ New With {.c=c.CustID,.r=c.Country} _ Equals New With _ {.c =o.CustID,.r =o.ShipCountry} _ Into j = Group _ From jo In j.DefaultIfEmpty() _ Select New With {c, jo} Association/relationship attributes _

Related docs
LINQ To SQL Cheat Sheet
Views: 194  |  Downloads: 29
LINQ CHEAT SHEET
Views: 9  |  Downloads: 3
LINQ to SQL
Views: 30  |  Downloads: 3
LINQ Standard Query Operators Cheat Sheet 2008
Views: 660  |  Downloads: 21
linq to sql beta 2 to rtm breaking changes
Views: 0  |  Downloads: 0
Linq
Views: 49  |  Downloads: 4
LINQ 2 SQL
Views: 57  |  Downloads: 0
LINQ to SQL Tutorial
Views: 2  |  Downloads: 0
DEVELOPING APPLICATIONS WITH LINQ 2 SQL
Views: 38  |  Downloads: 1
LINQ – Bringing Data into Visual Basic
Views: 0  |  Downloads: 0
LINQ 20 Democratizing the Cloud
Views: 0  |  Downloads: 0
premium docs
Other docs by Lucysiefker
Covenant Not to Compete
Views: 414  |  Downloads: 15
28novleft[0][0]
Views: 136  |  Downloads: 0
Municipal parking space rental permit
Views: 1240  |  Downloads: 2
employee_feedback_script
Views: 372  |  Downloads: 10
ALegal Lines _ Terms[0]
Views: 133  |  Downloads: 0
Transcript of Servicemens Readjustment Act
Views: 206  |  Downloads: 1
Partnership Agreement
Views: 1553  |  Downloads: 90
Word2003Basics_000
Views: 195  |  Downloads: 6
Option to Purchase Vacant Land
Views: 373  |  Downloads: 6
Sample Executive Summary Munninn Tech
Views: 197  |  Downloads: 1
Oakland NoticeToTenants
Views: 219  |  Downloads: 2
OPEN LISTING REALTY AGREEMENT
Views: 297  |  Downloads: 5