The following code finds the element greater than 5 in a 4*4 two-dimensional int array:
- Static void main (string [] ARGs)
- {
- Int [,] array = new int [4, 4];
- For (INT I = 0; I <4; I ++)
- For (Int J = 0; j <4; j ++)
- Array [I, j] = (I + 1) * (J + 1 );
- VaR intquery = from I in array. Cast <int> ()
- Where I> 5
- Select I;
- Foreach (int I in intquery)
- Console. writeline (I );
- }
Because the two-dimensional array cannot be directly converted to the ienumerable <t> type, the cast <t> () method first converts it to ienumerable <t> for query.
The enumerable. Cast <tresult> () generic method sets To the specified type.
This method is implemented by using delayed execution. The real-time return value is an object that stores all information required to perform operations. OnlyGetenumerator
Method or use foreach in Visual C # (or for each in Visual Basic) to enumerate the object, the query represented by this method is executed.
Cast <tresult> () (ienumerable)
By providing necessary type information, you can call the standard query operator on a non-generic set. For example, Not Implemented
But by calling Cast <tresult> () (ienumerable) on the object, you can use the standard query operator to query the sequence.
If the element cannot be forcibly converted to the tresult type, this method causes an exception. If you want to obtain only, you can forcibly convert it
For elements of the tresult type, use oftype <tresult> ()
Instead of Cast <tresult> () (ienumerable ).
In a query expression, an explicit Type Iteration variable is converted to a call of Cast <tresult> () (ienumerable.