最近学习LINQ资料不是很多,从CSHARPSAMPLE写一比较有用的例子方便大家学习。
1.INSERT/UPDATE/DELETE
insert Simple
public void LinqToSqlInsert02() {
Northwind db2 = new Northwind(connString);
DataLoadOptions ds = new DataLoadOptions();
ds.LoadWith<nwind.Category>(p => p.Products);
db2.LoadOptions = ds;
var q = (
from c in db2.Categories
where c.CategoryName == "Widgets"
select c);
Console.WriteLine("*** BEFORE ***");
ObjectDumper.Write(q, 1);
Console.WriteLine();
Console.WriteLine("*** INSERT ***");
var newCategory = new Category { CategoryName = "Widgets",
Description = "Widgets are the customer-facing analogues " +
"to sprockets and cogs."
};
var newProduct = new Product { ProductName = "Blue Widget",
UnitPrice = 34.56M,
Category = newCategory
};
db2.Categories.InsertOnSubmit(newCategory);
db2.SubmitChanges();
Console.WriteLine();
Console.WriteLine("*** AFTER ***");
ObjectDumper.Write(q, 1);
Cleanup65(); // Restore previous database state
}
public void LinqToSqlInsert03() {
Northwind db2 = new Northwind(connString);
DataLoadOptions ds = new DataLoadOptions();
ds.LoadWith<nwind.Employee>(p => p.EmployeeTerritories);
ds.LoadWith<nwind.EmployeeTerritory>(p => p.Territory);
db2.LoadOptions = ds;
var q = (
from e in db.Employees
where e.FirstName == "Nancy"
select e);
Console.WriteLine("*** BEFORE ***");
ObjectDumper.Write(q, 1);
Console.WriteLine();
Console.WriteLine("*** INSERT ***");
var newEmployee = new Employee { FirstName = "Kira",
LastName = "Smith"
};
var newTerritory = new Territory { TerritoryID = "12345",
TerritoryDescription = "Anytown",
Region = db.Regions.First()
};
var newEmployeeTerritory = new EmployeeTerritory { Employee = newEmployee,
Territory = newTerritory
};
db.Employees.InsertOnSubmit(newEmployee);
db.Territories.InsertOnSubmit(newTerritory);
db.EmployeeTerritories.InsertOnSubmit(newEmployeeTerritory);
db.SubmitChanges();
Console.WriteLine();
Console.WriteLine("*** AFTER ***");
ObjectDumper.Write(q, 2);
Cleanup66(); // Restore previous database state
}
Northwind db2 = new Northwind(connString);
DataLoadOptions ds = new DataLoadOptions();
ds.LoadWith<nwind.Employee>(p => p.EmployeeTerritories);
ds.LoadWith<nwind.EmployeeTerritory>(p => p.Territory);
db2.LoadOptions = ds;
var q = (
from e in db.Employees
where e.FirstName == "Nancy"
select e);
Console.WriteLine("*** BEFORE ***");
ObjectDumper.Write(q, 1);
Console.WriteLine();
Console.WriteLine("*** INSERT ***");
var newEmployee = new Employee { FirstName = "Kira",
LastName = "Smith"
};
var newTerritory = new Territory { TerritoryID = "12345",
TerritoryDescription = "Anytown",
Region = db.Regions.First()
};
var newEmployeeTerritory = new EmployeeTerritory { Employee = newEmployee,
Territory = newTerritory
};
db.Employees.InsertOnSubmit(newEmployee);
db.Territories.InsertOnSubmit(newTerritory);
db.EmployeeTerritories.InsertOnSubmit(newEmployeeTerritory);
db.SubmitChanges();
Console.WriteLine();
Console.WriteLine("*** AFTER ***");
ObjectDumper.Write(q, 2);
Cleanup66(); // Restore previous database state
}
update simple
update mutiple
public void LinqToSqlInsert06() {
Console.WriteLine("*** BEFORE ***");
ObjectDumper.Write(from c in db.OrderDetails where c.OrderID == 10255 select c);
Console.WriteLine();
Console.WriteLine("*** DELETE ***");
//Beverages
OrderDetail orderDetail = db.OrderDetails.First(c => c.OrderID == 10255 && c.ProductID == 36);
db.OrderDetails.DeleteOnSubmit(orderDetail);
db.SubmitChanges();
Console.WriteLine();
Console.WriteLine("*** AFTER ***");
ClearDBCache();
ObjectDumper.Write(from c in db.OrderDetails where c.OrderID == 10255 select c);
Cleanup69(); // Restore previous database state
}
Console.WriteLine("*** BEFORE ***");
ObjectDumper.Write(from c in db.OrderDetails where c.OrderID == 10255 select c);
Console.WriteLine();
Console.WriteLine("*** DELETE ***");
//Beverages
OrderDetail orderDetail = db.OrderDetails.First(c => c.OrderID == 10255 && c.ProductID == 36);
db.OrderDetails.DeleteOnSubmit(orderDetail);
db.SubmitChanges();
Console.WriteLine();
Console.WriteLine("*** AFTER ***");
ClearDBCache();
ObjectDumper.Write(from c in db.OrderDetails where c.OrderID == 10255 select c);
Cleanup69(); // Restore previous database state
}
delete one to many
where
public void LinqToSqlWhere02() {
var q =
from e in db.Employees
where e.HireDate >= new DateTime(1994, 1, 1)
select e;
ObjectDumper.Write(q);
}
var q =
from e in db.Employees
where e.HireDate >= new DateTime(1994, 1, 1)
select e;
ObjectDumper.Write(q);
}
first condition
public void LinqToSqlSelect01() {
var q =
from c in db.Customers
select c.ContactName;
ObjectDumper.Write(q);
}
var q =
from c in db.Customers
select c.ContactName;
ObjectDumper.Write(q);
}
public void LinqToSqlSelect04() {
var q =
from p in db.Products
select new {p.ProductID, HalfPrice = p.UnitPrice / 2};
ObjectDumper.Write(q, 1);
}
var q =
from p in db.Products
select new {p.ProductID, HalfPrice = p.UnitPrice / 2};
ObjectDumper.Write(q, 1);
}
public void LinqToSqlSelect05() {
var q =
from p in db.Products
select new {p.ProductName, Availability = p.UnitsInStock - p.UnitsOnOrder < 0 ? "Out Of Stock": "In Stock"};
ObjectDumper.Write(q, 1);
}
var q =
from p in db.Products
select new {p.ProductName, Availability = p.UnitsInStock - p.UnitsOnOrder < 0 ? "Out Of Stock": "In Stock"};
ObjectDumper.Write(q, 1);
}
public void LinqToSqlSelect10() {
var q = (
from c in db.Customers
select c.City )
.Distinct();
ObjectDumper.Write(q);
}
var q = (
from c in db.Customers
select c.City )
.Distinct();
ObjectDumper.Write(q);
}
public void LinqToSqlCount01() {
var q = db.Customers.Count();
Console.WriteLine(q);
}
public void LinqToSqlCount02() {
var q = db.Products.Count(p => !p.Discontinued);
Console.WriteLine(q);
}
public void LinqToSqlCount03() {
var q = db.Orders.Select(o => o.Freight).Sum();
Console.WriteLine(q);
}
public void LinqToSqlCount05() {
var q = db.Products.Select(p => p.UnitPrice).Min();
Console.WriteLine(q);
}
public void LinqToSqlCount07() {
var categories =
from p in db.Products
group p by p.CategoryID into g
select new {
CategoryID = g.Key,
CheapestProducts =
from p2 in g
where p2.UnitPrice == g.Min(p3 => p3.UnitPrice)
select p2
};
ObjectDumper.Write(categories, 1);
}
public void LinqToSqlCount08() {
var q = db.Employees.Select(e => e.HireDate).Max();
Console.WriteLine(q);
}
public void LinqToSqlCount11() {
var q = db.Orders.Select(o => o.Freight).Average();
Console.WriteLine(q);
}
public void LinqToSqlCount12() {
var q = db.Products.Average(p => p.UnitPrice);
Console.WriteLine(q);
}
public void LinqToSqlCount13() {
var categories =
from p in db.Products
group p by p.CategoryID into g
select new {
g.Key,
ExpensiveProducts =
from p2 in g
where p2.UnitPrice > g.Average(p3 => p3.UnitPrice)
select p2
};
ObjectDumper.Write(categories, 1);
}
var q = db.Customers.Count();
Console.WriteLine(q);
}
public void LinqToSqlCount02() {
var q = db.Products.Count(p => !p.Discontinued);
Console.WriteLine(q);
}
public void LinqToSqlCount03() {
var q = db.Orders.Select(o => o.Freight).Sum();
Console.WriteLine(q);
}
public void LinqToSqlCount05() {
var q = db.Products.Select(p => p.UnitPrice).Min();
Console.WriteLine(q);
}
public void LinqToSqlCount07() {
var categories =
from p in db.Products
group p by p.CategoryID into g
select new {
CategoryID = g.Key,
CheapestProducts =
from p2 in g
where p2.UnitPrice == g.Min(p3 => p3.UnitPrice)
select p2
};
ObjectDumper.Write(categories, 1);
}
public void LinqToSqlCount08() {
var q = db.Employees.Select(e => e.HireDate).Max();
Console.WriteLine(q);
}
public void LinqToSqlCount11() {
var q = db.Orders.Select(o => o.Freight).Average();
Console.WriteLine(q);
}
public void LinqToSqlCount12() {
var q = db.Products.Average(p => p.UnitPrice);
Console.WriteLine(q);
}
public void LinqToSqlCount13() {
var categories =
from p in db.Products
group p by p.CategoryID into g
select new {
g.Key,
ExpensiveProducts =
from p2 in g
where p2.UnitPrice > g.Average(p3 => p3.UnitPrice)
select p2
};
ObjectDumper.Write(categories, 1);
}
join
public void LinqToSqlPaging01() {
var q = (
from c in db.Customers
orderby c.ContactName
select c)
.Skip(50)
.Take(10);
ObjectDumper.Write(q);
}
var q = (
from c in db.Customers
orderby c.ContactName
select c)
.Skip(50)
.Take(10);
ObjectDumper.Write(q);
}