using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LINQTest
{
class Program
{
static void Main(string[] args)
{
List<int> myList = new List<int>() { 3, 4, 2, 7, 9, 0, 8, 1, 5, 6 };
var filterList = myList.Where(c => c > 5);
foreach (var filteritem in filterList)
{
Console.WriteLine(filteritem);
}
Console.ReadKey();
}
}
}
上例橘色區塊是把myList中,數值大於5的挑出來,並印到console
你可以把橘色區塊換成
myList.Where(c => c > 5 && c < 8);
那它就會挑出數值大於5並且小於8的數字
另一種用法是這樣的
myList.Where(c => c > 5).Where(c => c < 8);
一樣是挑出數值大於5並且小於8的數字
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LINQTest
{
class Program
{
static void Main(string[] args)
{
List<string> myList = new List<string>() { "ab", "aczzz", "bcyyyy", "bdxxxxx", "cewwwwww", "cfvvvvvvv", "efuu", "egttt", "fgss", "fh" };
var filterList = myList.Where(c => c.Contains("a"));
foreach (var filteritem in filterList)
{
Console.WriteLine(filteritem);
}
Console.ReadKey();
}
}
}
如果我們把它換成文字的搜尋,上例橘色區塊就會挑出字串中含有a的
搜尋條件改成
myList.Where(c => c == "aczzz");
那就會完全比對正確的字串
搜尋條件改成
myList.Where(c => c.Length > 5);
它就會比對每個字串長度大於5的字串
留言列表