A subquery is a query contained in another query's lambda expression.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
string[] names = { "Java a", "C# c", "Javascript t" };
IEnumerable<string> query = names.OrderBy(m => m.Split().Last());
foreach (string s in query)
{
Console.WriteLine(s);
}
}
}
The output:
Java a
C# c
Javascript t
Get all strings with the shortest length:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
string[] names = { "Java", "C#", "Javascript" };
IEnumerable<string> outerQuery = names
.Where(n => n.Length == names.OrderBy(n2 => n2.Length)
.Select(n2 => n2.Length).First());
foreach (string s in outerQuery)
{
Console.WriteLine(s);
}
}
}
The output:
C#
Here's the same thing as a query expression:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
string[] names = { "Java", "C#", "Javascript" };
IEnumerable<string> outerQuery =
from n in names
where n.Length == (from n2 in names orderby n2.Length select n2.Length).First()
select n;
foreach(string s in outerQuery){
Console.WriteLine(s);
}
}
}
The output:
C#
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. |