Barner
  
YAZAR :Emin Gökçe
ALANI :Php, Mysql
Linq to Sql İçin Bilinmesi Gereken Komutar

 Data context & database create

var db = new MyDataContext(@"server=.SQLEXPRESS;database=my;integrated security=SSPI");

if (!db.DatabaseExists()) db.CreateDatabase();

Where, null, contains & type

var r = new string[] { "WA", "OR" };

var customers = from in db.Customers

where is Customer &&

(c.Region==null || r.Contains(c.Region))

select c;

Insert

var customer = new Customer() {

CustID = "CHIPS",

CompanyName = "Mr. Chips" };

db.Customers.InsertOnSubmit(customer);

db.SubmitChanges();

Update

customer.ContactName = "Adam";

customer.Location = null;

db.SubmitChanges();

Select one

var only = db.Customers.SingleOrDefault(c

=> c.CustID == "CHIPS");

var first = db.Customers.FirstOrDefault(c

=> c.CustID == "CHIPS");

Paging & order

var page3 = (from in db.Customers

orderby c.ContactName, c.City descending

select c).Skip(10).Take(5);

Delete

db.Customers.DeleteOnSubmit(customer);

db.SubmitChanges();

Group, count, sum & projection

var totals = from in db.Customers

group by c.Country into g

select new Summary { Country = g.Key,

CustomerCount = g.Count(),

OrdCount = g.Sum(a=> a.Orders.Count)};

Join, shape & distinct

var labels = (from in db.Customers

join in db.Orders

on c.CustID equals o.CustID

select new { name = c.ContactName,

address = o.ShipAddress

}).Distinct();

Class/table attributes

[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)]

Association/relationship attributes

[Association(Name="Country_Customer",

Storage="_Country", ThisKey="CoID",

IsForeignKey=true)]

Composite & outer join

var r = from in db.Customers

join 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 };

 

 

Alıntı...