Query two-dimensional arrays in LINQ

Source: Internet
Author: User
The following code finds the element greater than 5 in a 4*4 two-dimensional int array:
  1. Static void main (string [] ARGs)
  2. {
  3. Int [,] array = new int [4, 4];
  4. For (INT I = 0; I <4; I ++)
  5. For (Int J = 0; j <4; j ++)
  6. Array [I, j] = (I + 1) * (J + 1 );
  7. VaR intquery = from I in array. Cast <int> ()
  8. Where I> 5
  9. Select I;
  10. Foreach (int I in intquery)
  11. Console. writeline (I );
  12. }

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.