Kamis, 02 September 2010

Contoh-Contoh Query LINQ

Contoh 1

public void SimpleQuery()

{

DataClasses1DataContext dc = new DataClasses1DataContext();

var q =

from a in dc.GetTable<Order>()

select a;

dataGridView1.DataSource = q;

}




Contoh 2

public void SimpleQuery2()
{
DataClasses1DataContext dc = new DataClasses1DataContext();
dataGridView1.DataSource = dc.GetTable<Order>();
}



Contoh 3

public void SimpleQuery3()

{

DataClasses1DataContext dc = new DataClasses1DataContext();

var q =

from a in dc.GetTable<Order>()

where a.CustomerID.StartsWith("A")

select a;

dataGridView1.DataSource = q;

}



Contoh 4

public void GetCustomerOrder()

{

DataClasses1DataContext dc = new DataClasses1DataContext();

var q= (from orders in dc.GetTable<Order>()

from orderDetails in dc.GetTable<Order_Detail>()

from prods in dc.GetTable<Product>()

where ((orderDetails.OrderID == orders.OrderID) &&

(prods.ProductID == orderDetails.ProductID) &&

(orders.EmployeeID == 1))

orderby orders.ShipCountry

select new CustomerOrderResult

{

CustomerID = orders.CustomerID,

CustomerContactName = orders.Customer.ContactName,

CustomerCountry = orders.Customer.Country,

OrderDate = orders.OrderDate,

EmployeeID = orders.Employee.EmployeeID,

EmployeeFirstName = orders.Employee.FirstName,

EmployeeLastName = orders.Employee.LastName,

ProductName = prods.ProductName

}).ToList<CustomerOrderResult>();

dataGridView1.DataSource = q;

}




sumber : http://www.c-sharpcorner.com/

Tidak ada komentar: