Ienumerable <t> is commonly used in Windows Phone 7 programs. It allows developers to define the implementation of the foreach statement function and supports simple iteration of non-generic methods, the following mainly analyzes ienumerable <t>. select and ienumerable <t>. the difference between the selectmany methods.
Ienumerable <t>. select projects each element in the sequence to the new table.
Ienumerable <t>. selectsequence projects each element of the sequence to ienumerable <t> and merges the result sequence into a sequence.
The selectmany method is used to enumerate input sequences, map each element to ienumerable <t> using the conversion function, and then enumerate and generate each element of the ienumerable <t> object. That is to say, for each element of the source, the selector is called and a sequence of values is returned. Then selectsets combines the two-dimensional set of the set into one-dimensional ienumerable <t> and returns it.
The following small example uses ienumerable <t>. Select and ienumerable <t>. selectiterator to implement the same function. Let's see the difference between the two.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox Name="Output"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Margin="10" />
<ListBox Name="Output2"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Margin="200,10,10,10" />
</Grid>
CS page
Namespace linqapplication
{
Public partial class mainpage: phoneapplicationpage
{
List <book> books; // list <t> inherits the ienumerable <t> Interface
Public mainpage ()
{
Initializecomponent ();
Createbooks ();
// Ienumerable <book>. select projects the authors element in the sequence to the new table.
Ienumerable <list <author> enumerableoflistofauthors = books. Select (Book => book. Authors );
Foreach (VAR listofauthors in enumerableoflistofauthors)
{
Foreach (author auth in listofauthors)
{
Output. Items. Add (Auth. Name); // Add it to ListBox
}
}
// Ienumerable <book>. selectsequence projects each element of the sequence to ienumerable <t> and merges the result sequence into a sequence.
Ienumerable <author> authors = books. selecttables (Book => book. Authors );
Foreach (author auth in authors)
{
Output2.items. Add (Auth. Name );
}
}
Private void createbooks ()
{
Books = new list <book> ();
Author auth1 = new author () {name = "Zhang San", age = 32 };
Author auth1 = new author () {name = "", age = 30 };
Author auth3 = new author () {name = "Garfield", age = 31 };
List <author> authors = new list <author> () {auth1, au2, auth3 };
Book newbook = New Book () {authors = authors, numpages = 500, Title = "programming C #"};
Books. Add (newbook); auth1 = new author () {name = "Andy Lau", age = 42 };
Authors = new list <author> () {auth1 };
Newbook = New Book () {authors = authors, numpages = 350, Title = "Book 2 "};
Books. Add (newbook); auth1 = new author () {name = "Jay Chou", age = 32 };
Auth1 = new author () {name = "Lin Zhiling", age = 32 };
Authors = new list <author> () {auth1, au22 };
Newbook = New Book () {authors = authors, numpages = 375, Title = "programming with WP7 "};
Books. Add (newbook );
}
}
}
Author. CS class
namespace LinqApplication
{
public class Author
{
public string Name;
public int Age;
}
}
Book. CS class
namespace LinqApplication
{
public class Book
{
public String Title { get; set; }
public List<Author> Authors { get; set; }
public int NumPages { get; set; }
}
}