C#Linq语法

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值