1、简单查询int[] nums = { 1, 2, 5, -4, -9, 78, -54, 98 };
需要注意的是:
Linq查询只是定义了查询规则,执行可以多次进行,where条件可以有多个(以“&&”连接或者直接下行再写where)
//LinQ查询都是以From开始,以Select或者group结束
//LinQ的过程:
1、创建查询
2、执行查询
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace demo1
{
class Program
{
static void Main(string[] args)
{
int[] nums = { 1, 2, 5, -4, -9, 78, -54, 98 };
//创建查询
var dictionaries = from n in nums
where n > 0&&n<10
select n;
StringBuilder sb = new StringBuilder();
//执行查询
foreach (var item in dictionaries)
{
sb = sb.AppendFormat("{0},",item);
}
Console.WriteLine(sb);
string[] webUrl = { "www.A-com.com", "www.A-org.org", "www.A-cn.cn", "www.B-com.com", "www.C-com.com",
"www.B-org.org" };
var comUrl=from url in webUrl
where url.EndsWith(".com")
select url;
StringBuilder sbUrl = new StringBuilder();
foreach (var item in comUrl)
{
sbUrl = sbUrl.AppendFormat("{0},",item);
}
Console.WriteLine(sbUrl);
Console.Read();
}
}
}
结果:
2、