Input: | IEnumerable<TSource> |
Lambda expression: | TSource => bool or (TSource,int) => bool |
Query syntax: | where boolExpression |
Where
returns the elements that satisfy the given expression.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
string[] names = { "Java", "C#", "Javascript", "SQL", "Oracle", "Python", "C++", "C", "HTML", "CSS" };
IEnumerable<string> query = names.Where(name => name.EndsWith("e"));
foreach (string s in query)
{
Console.WriteLine(s);
}
}
}
The output:
Oracle
In query syntax:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
string[] names = { "Java", "C#", "Javascript", "SQL", "Oracle", "Python", "C++", "C", "HTML", "CSS" };
IEnumerable<string> query = from n in names where n.EndsWith("e") select n;
foreach (string s in query)
{
Console.WriteLine(s);
}
}
}
The output:
Oracle
A where clause can appear more than once in a query:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
string[] names = { "Java", "C#", "Javascript", "SQL", "Oracle", "Python", "C++", "C", "HTML", "CSS" };
IEnumerable<string> query =
from n in names
where n.Length > 3
let u = n.ToUpper()
where u.EndsWith("E")
select u;
foreach (string s in query)
{
Console.WriteLine(s);
}
}
}
The output:
ORACLE
Indexed filtering where expression
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
string[] names = { "Java", "C#", "Javascript", "SQL", "Oracle", "Python", "C++", "C", "HTML", "CSS" };
IEnumerable<string> query = names.Where((n, i) => i % 2 == 0);
foreach (string s in query)
{
Console.WriteLine(s);
}
}
}
The output:
Java
Javascript
Oracle
C++
HTML
java2s.com | Contact Us | Privacy Policy |
Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
All other trademarks are property of their respective owners. |