LinQ

用于获取数据源
var queryAllCustomers=from cust in Customers select cust;
需求:查询出成绩有90分以上的学生,得到他们的名字和成绩
//数据源
IList<Student> students = new List<Student>
{
new Student{ Name="Kevin", Score=new List<int>{89,93,88,78}},
new Student{ Name="Jackie",Score=new List<int>{92,87,83,91}},
new Student{ Name="Helen",Score=new List<int>{53,76,72,62}}
};
//使用复合from子句查询命令
var getStudent =
from student in students
from score in student.Score
where score > 90
select new { Name=student.Name,Score=score};

使用where筛选在伦敦的客户
var q =
from c in db.Customers
where c.City == "London"
select c;

再如:筛选1994 年或之后雇用的雇员:
var q =
from e in db.Employees
where e.HireDate >= new DateTime(1994, 1, 1)
select e;

筛选库存量在订货点水平之下但未断货的产品
var q =
from p in db.Products
where p.UnitPrice > 10m || p.Discontinued
select p;

First()形式: 返回集合中的一个元素,其实质就是在SQL语句中加TOP (1)
Shipper shipper = db.Shippers.First();
元素:选择CustomerID 为“BONAP”的单个客户
Customer cust = db.Customers.First(c => c.CustomerID == "BONAP");
条件:选择运费大于 10.00 的订单:

Order ord = db.Orders.First(o => o.Freight > 10.00M);

需求:将数组中小于5的偶数查询出来输出到控制台

复制代码
//数据源
int[] arr = { 0, 3, 2, 1, 9, 6, 8, 7, 4, 5 };

//使用Where子句查询的查询语句
var query = from a in arr
where a < 5 && a % 2 == 0
select a;

//执行查询
foreach (var a in query)
{
Console.WriteLine(a);
}

 //两表关联

//var list = from bdl in bodyDataList
// join hdl in hesdDataList on bdl.ItemNo equals hdl.ItemNo into temp
// select new ZTOutDelivery
// {
// DeliveryOrderNo = bdl.DeliveryOrderNo,
// DeliveryOrderItemNo = bdl.ItemListNo,
// ItemNo = bdl.ItemNo,
// ItemListNo = bdl.DeliveryOrderItemNo,
// CopGNo = bdl.CopGNo,
// CreateDate = temp.Select(t => t.CreateDate).FirstOrDefault(),
// CreateUserName = temp.Select(t => t.CreateName).FirstOrDefault(),
// QTY = bdl.Qty
// };
//foreach (var item in bodyDataList)
//{
// DataTable GetByNOResult = GetByNOSearch(item.DeliveryOrderNo, item.ItemListNo);
// if (GetByNOResult.Rows.Count > 0)
// {

// }
// else {

// }
//}

原文地址:https://www.cnblogs.com/lacey/p/14683837.html