Quantifier Operations
Quantifier operations produce a Boolean value to indicate whether all or a portion of elements satisfy a specific condition.
LINQ performs these operations with three methods: All, Any, and Contains.
The All method determines if all elements within a sequence satisfy the given condition. Review its syntax below:
public static bool All<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate )
Review an example of its use:
class Rides
{
public string Name { get; set; }
public int Year { get; set; }
}
public static void Examples()
{
// spawn array of rides
Rides[] rides = { new Rides { Name="Chevy", Year=17 },
new Rides { Name="Porche", Year=66 },
new Rides { Name="Pontiac", Year=64 }
};
// Gather names starting with 'P'
bool StartWB = rides.All(ride => ride.Name.StartsWith("P"));
Console.WriteLine(
"{0} pet names start with 'P'.",
StartWB ? "All" : "Not all");
}
The Boolean result of the All method typically appears within a where clause or in a direct call to the Where method. Review an example of this usage below:
class ShowRide
{
public string Name { get; set; }
public int Year { get; set; }
}
class Competitor
{
public string SurName { get; set; }
public ShowRide[] ShowRides { get; set; }
}
public static void Examples()
{
List<Competitor> competitor = new List<Competitor>{
new Competitor{
SurName = "Smith",
ShowRides = new ShowRide[] {
new ShowRide { Name="Bonneville", Year=64 },
new ShowRide { Name="Nova", Year=64 },
new ShowRide { Name="SS Impala", Year=63 }
}
},
new Competitor {
SurName = "Appiah",
ShowRides = new ShowRide[] {
new ShowRide { Name = "Chevelle", Year = 66 }
}
},
new Competitor {
SurName = "Shingledecker",
ShowRides = new ShowRide[] {
new ShowRide { Name ="Corvette", Year = 79 }
}
},
new Competitor {
SurName = "Brown",
ShowRides = new ShowRide[] {
new ShowRide { Name = "Belair", Year = 55 },
new ShowRide { Name = "Transam", Year = 77 }
}
}
};
// Determine competitors with cars newer than 1975
IEnumerable<string> names = from competitor in competitors
where competitor.ShowRides.All(showride => showride.Year > 75)
select competitor.SurName;
foreach (string name in names)
{
Console.WriteLine(name);
}
The Any method determines the presence of any sequence elements satisfying a given condition. Review its syntax below:
public static bool Any<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate )
Review an example of its use:
class Weapon
{
public string Name { get; set; }
public int Size { get; set; }
public bool Upgraded { get; set; }
}
public static void Examples()
{
// make weapon array
Weapon[] weapons =
{ new Weapon { Name="Destroyer", Size=9, Upgraded=true },
new Weapon { Name="Rattler", Size=2, Upgraded=false },
new Weapon { Name="Blaster", Size=5, Upgraded=false } };
// find weapons over size 1 without an upgrade
bool noupgrade = weapons.Any(w => w.Size > 1 && w.Upgraded == false);
Console.WriteLine(
"There are {0} weapons over size one without an upgrade.",
noupgrade ? "are" : "are not any");
}
The Contains method determines if a sequence contains the element specified. Review its syntax below:
public static bool Contains<TSource>( this IEnumerable<TSource> source, TSource value )
Review an example of its use:
string[] candies = { "gummi", "chocolate", "lollipop", "gum", "sourpower", "chews" };
string candy = "poprocks";
bool haspoprocks = candies.Contains(candy);
Console.WriteLine(
"The array {0} contains '{1}'.", haspoprocks ? "does" : "does not", candy);