์ค๋๋ ์ ์ ๋นก๋๊ฐ๋ฆฌ๊ฐ ๋์๊ฐ์ง๋ฅผ ์๋ค์ใ ใ
List<int> a = new List<int>();
a.Add(1);
a.Add(2);
a.Add(3);
List<int> b = new List<int>();
b.Add(4);
b.Add(5);
b.Add(6);
์๋ ๊ฒ ๋๊ฐ์ ๋ฆฌ์คํธ๊ฐ ์๋ค๊ณ ํ ๋
List a๊ฐ 1, 2, 3์ด๊ณ List b๊ฐ 4, 5, 6 ์ด๋ ๊ฒ ๋ ํ ๋ฐ
์๋ก์ด List c๋ฅผ ์ค์ ํด์ 5, 7, 9๊ฐ ๋์ค๊ฒ๋ ํ ์ ์์๊น์?
์์ฝ
๊ฐ์ ์์์ ์๋ ์ฑ๋ถ๋ค์ ๊ฐ๊ฐ ๋ํด์ ์๋ก์ด ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค ์ ์๋ ๊ธฐ๋ฅ์ด ํ์ํฉ๋๋คใ
ใ
using System.Linq ์ฌ์ฉํด์ var c = a.Zip(b, (first, second) => first + second).ToList();
๋ Linq ๊ณต๋ถํ๊ณ ๋์ ์ฐ๋๊ฒ ์ข์๊ฑฐ๊ฐ๊ณ ์ง๊ธ์ ๊ฑ for๋ฌธ ๋๋ ค์ ํฉ์น์ธ์ฌ...
for( int i=0; i<a.Count; i++) { c.Add(a[i]+b[i]); } ์ด๋ ๊ฒ ํ๋ฉด ๋๊ฒ ๋ค
Linq๊ฐ ๋ญ์ง๊น์ง๋ ์ ๋ชจ๋ฅด๊ฒ ๋ค์...ใ ใ for ์ฌ์ฉํด์ ํ๋ฒ ํด๋ณผ๊ฒ์ ๊ฐ์ฌํฉ๋๋ค!!