Finding if a target number is the sum of two numbers in an array via LINQ and get the and Indices
Posted
by Dr.H
on Stack Overflow
See other posts from Stack Overflow
or by Dr.H
Published on 2010-04-10T03:40:13Z
Indexed on
2010/04/10
3:43 UTC
Read the original article
Hit count: 370
Hello I am new to Linq , I found this thread which explain 90% of what I need http://stackoverflow.com/questions/2331882?tab=newest#tab-top , thanks "pdr"
but what I need is to get the Indices too , here is my modification I get the index of the first number but I don't know how to get the index of the second number
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var result = from item in numbers.Select((n1, idx) =>
new { n1,idx, shortList = numbers.Take(idx) })
from n2 in item.shortList
where item.n1 + n2 == 7
select new { nx1 = item.n1,index1=item.idx, nx2=n2 };
© Stack Overflow or respective owner