Đỉnh NGUYỄN

life's a journey not a destination

Dùng LINQ tính tổng mảng nhiều chiều

Leave a comment


Đây là cách tính tổng 1 mảng nhiều chiều dùng LINQ. Mảng này có 3 dòng & 2 cột.

using System;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var myArray = new int[,] {
                    { 1, 2 },
                    { 3, 4 },
                    { 5, 6 }
                };
                var arrSum = (from int val in myArray select val).Sum();

                Console.WriteLine(arrSum.ToString());
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                // handle ex
            }
        }
    }
}

Advertisements

Author: dinhnn

Senior software developer, a technical leader. You can be reached at via email to dinhnguyenngoc@gmail.com, via my blog at dinhnguyenngoc.wordpress.com, and on Twitter @dinhnguyenngoc.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s