1、
ZKWebForm1219_20171128Entities db = new ZKWebForm1219_20171128Entities();
protected void Button1_Click(object sender, EventArgs e)
{
//where:简单形式=================
var qy = from c in db.city
where c.name == "武汉市"
select c;
foreach (var im in qy)
{
//Response.Write(im.name+"====="+im.provinceId+"=====");
//Response.Write("<br />");
}
//筛选
var q = from st in db.LbtProjectInfo
where st.ID < 600
select st;
foreach (var item in q)
{
//Response.Write(item.ID+" "+item.ProjectName+"====="+item.ContractNumber+"=====");
//Response.Write("<br />");
}
//筛选:关系条件形式=================
var qyd = from sct in db.LbtProjectInfo
where sct.ID > 500 && sct.ID < 550
select sct;
foreach (var iem in qyd)
{
//Response.Write(iem.ID + " " + iem.ProjectName + "=====" + iem.ContractNumber + "=====");
//Response.Write("<br />");
}
Response.Write("<br />");
//var qyd2 = db.LbtProjectInfo.Where(p => p.ID > 500).Where(p => p.ID < 550);
var qyd2 = db.LbtProjectInfo.Where(p => p.ID > 500 && p.ID < 550);
foreach (var iem2 in qyd2)
{
//Response.Write(iem2.ID + " " + iem2.ProjectName + "=====" + iem2.ContractNumber + "=====");
//Response.Write("<br />");
}
//First():形式(返回序列中第一个元素)
LbtProjectInfo sp=db.LbtProjectInfo.First();
//Response.Write(sp.ID + " " + sp.ProjectName + "=====" + sp.ContractNumber + "=====");
LbtProjectInfo spw = db.LbtProjectInfo.First(c => c.ID == 547);
//Response.Write(spw.ID + " " + spw.ProjectName + "=====" + spw.ContractNumber + "=====");
}
//匿名类型
var qam = from sect in db.LbtProjectInfo
where sect.ID < 550
select new { sect.ID, sect.ProjectName };
foreach (var ist in qam)
{
//Response.Write(ist.ID + " " + ist.ProjectName);
//Response.Write("<br />");
}
//指定类型形式
var qamd = from y in db.LbtProjectInfo
where y.ID < 550
select new
{
ID = y.ID,
PN = y.ProjectName
};
2、拉姆语法:
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
IEnumerable<string> query2 = names
.Where(n => n.Contains("a")) //n直接来自names array
.OrderBy(n => n.Length) //n来自filter之后的subsequent
.Select(n => n.ToUpper()); //n来自OrderBy之后的subsequent