您的位置:首页 > 编程语言 > ASP

Linq自己不懂的知识总汇 Lambda表达式 select 与 where的选择

2013-07-06 09:21 435 查看
1)有一段时间linq的 lambda表达式什么时候需要使用 where语句 和 select 语句一直搞不懂,什么时候用where什么时候用select,所以绝大多数我都是在尝试一个不行我就换另一个。直到今天细细的品味了一下,才发现什么时候该用where什么时候该用select。其实也挺简单的,就是假如后面的表达式返回的是true 或者false的bool值的时候就用where,要是后面能直接得到值就用select,代码如下:

 注:下面的range(10,200)是生成一个从10 开始,然后数目长度为200的整数数组。(Enumerable 还有repeate方法是产生重复的整数数组的方法)

using System;
using System.Linq;

namespace Test
{
public partial class lin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int[] ii = Enumerable.Range(10, 200).Where(i => i % 10 == 0).ToArray();
foreach (int i in ii)
{
Response.Write(i.ToString()+"<br/>");
}
}
}
}


上面的是判断有没有被10整除的整数,如果这个条件是“真”(true)的,那么就返回,假如为“假”(false)就不返回。-------->所以要使用where

而下面的这个例子要使用select,实例如下:

using System;
using System.Linq;

namespace Test
{
public partial class lin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int[] ii = Enumerable.Range(10, 200).Select(i => i / 10).ToArray();
foreach (int i in ii)
{
Response.Write(i.ToString()+"<br/>");
}
}
}
}


上面的就是把每一个i除以10 然后返回。没有什么true 或者false的条件判断,所以这个时候我们应该选择select方法。

 

2)今天看见了一个linq语句,其中的&&(即并且不是用并且而是用了两个where连接的,我是第一次看到写两个where)实例如下:

using System;
using System.Linq;
using System.Collections.Generic;

namespace Test
{
public partial class lin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int[] ii = Enumerable.Range(1, 10).ToArray();
var a = from jj in ii where jj>5 where jj<10 select jj;
foreach (var ab in a)
{
Response.Write(ab.ToString());
}
}
}
}


注:linq语句与lambda不能共存(自己见解不知道对不对)

 

3)Linq中假如要选择多个字段,那么操作如下:

using System;
using System.Linq;
using System.Collections.Generic;

namespace Test
{
public partial class lin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var c =new [] { new { ContactName = "jj", Phone ="1234567"},new { ContactName = "jju", Phone ="123456u"}};
var d = from fr in c where fr.ContactName == "jj" select new {fr.ContactName,fr.Phone };
foreach (var a in d)
{
Response.Write("联系名字:" +a.ContactName+"<br/>联系电话:"+a.Phone);
}
}
}
}


注意上面的select 语句,select new {fr.ContactName,fr.Phone }; 这就是选择多个字段的方法,假如要选择总体的字段 直接select fr就可以了。

同时我们还可以通过select 来起别名,如select name as 姓名 from userinfo 一样,实例如下:

using System;
using System.Linq;
using System.Collections.Generic;

namespace Test
{
public partial class lin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var c =new [] { new { ContactName = "jj", Phone ="1234567"},new { ContactName = "jju", Phone ="123456u"}};
var d = from fr in c where fr.ContactName == "jj" select new { na = fr.ContactName+fr.Phone };
foreach (var a in d)
{
Response.Write("合成之后:" + a.na + "<br/>");
}
}
}
}

上面的代码涉及到起别名和合并,使用起来比较方便。上面的合并的操作也可变成其他的如一个字段的处理如除法、乘法等。简单实例如下:

using System;
using System.Linq;
using System.Collections.Generic;

namespace Test
{
public partial class lin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var c =new [] { new { ContactName = "jj", Phone =1234567},new { ContactName = "jju", Phone =987}};
var d = from fr in c where fr.ContactName == "jj" select new { na = fr.Phone/2 };
foreach (var a in d)
{
Response.Write("合成之后:" + a.na.ToString() + "<br/>");
}
}
}
}

 

-----------------------------------未完待续-------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LINQ ASP.NET