0

I want do Linq like in topic. I have List<double> i want to check there is 2 different double which sum is more then for example 20. i don't have idea how to do that. I create something like this

tags.Any(x => (x.Distance + tags.Select(y => y.Distance).First() > 20));

But it check only with first object, and check with myself. If 2 elements on list are the same they can be check, but with myself can't.

Can i do something like take this and next object from list?

var tags = new List<double>() {
  5, 7, 9, 13 
};

if ((i != j) && (tags(i) + tags(j)) > 20)
{

}

This link should work like this if when it would be array in a loop.

9
  • 1
    What is tags? Can you show the data-structure and some sample-data as well as expected behaviour? Commented Oct 2, 2019 at 13:01
  • 1
    Could you provide an example? (Both initial data and desired outcome) Commented Oct 2, 2019 at 13:01
  • tags are List<double>. I can 1 second Commented Oct 2, 2019 at 13:03
  • I think you want tags.SelectMany(t1 => tags.Where(t2 => t2 != t1).Select(t2 => t1.Distance + t2.Distance)).Any(d => d > 20) Commented Oct 2, 2019 at 13:07
  • 1
    @404 That not going to compile since the result of a Select is an IEnumerable, but Any requires a predicate that returns a bool. Commented Oct 2, 2019 at 13:10

2 Answers 2

3

Try Cartesian Join (tags with itself):

var tags = new List<double>() {
  5, 7, 9, 13
};

var result =  tags
  .Select((a, index_a) => tags
     .Where((b, index_b) => index_a != index_b && a + b > 20)
     .Select(b => Tuple.Create(a, b)))
  .SelectMany(item => item);

// Let's have a look:
Console.Write(string.Join(Environment.NewLine, result));

Outcome:

(9, 13)
(13, 9)

if you don't want (13, 9) and alike pairs (where index_a > index_b)

var result = tags
  .Select((a, index_a) => tags
    .Skip(index_a + 1)
    .Where(b => a + b > 20)
    .Select(b => Tuple.Create(a, b)))
 .SelectMany(item => item);

// Let's have a look:
Console.Write(string.Join(Environment.NewLine, result)); 

Outcome:

(9, 13)
Sign up to request clarification or add additional context in comments.

Comments

0

Linq is a fancy cover for for loops, foreach loops and conditional statements, as result I would construct the above question in terms of for loops and conditionals etc, then afterwards look to convert this into a Linq statement.

e.g

private List<Tuple<double, double>> GetElementsWhosSumIsGreaterThanX(List<double> originalList, double x)
    {
        //take the absolute value of the variable to eliminate sign errors
        x= Math.Abs(x);

        List<Tuple<double, double>> returnList = new List<Tuple<double, double>>();

        //only go upto count - 1 as there is no more elements to compare the last element to
        for (int i = 0; i < originalList.Count - 1; i++)
        {//loop over each element in turn
            for (int j = i; j < originalList.Count - i; j++)
            {//compare it to the rest of the list from the ith element on               
                double sum = originalList[i] + originalList[j];

                if (sum> x)
                    returnList.Add( new Tuple<double, double>(originalList[i], originalList[j]));
            }
        }

        return returnList;
    }

This can be used like:

public void Test()
    {
        List<double> tags = new List<double>() {5, 7, 9, 13, 10.1, 25.6, 32.1};

        List<Tuple<double, double>> results = GetElementsWhosSumIsGreaterThanX(tags, 20);

        for (int i = 0; i < results.Count; i++)
    {
        Console.WriteLine("{0} and {1} have a sum greater than 20", results[i].Item1, results[i].Item2);
    }

    /*output:
    5 and 25.6 have a sum greater than 20
    5 and 32.1 have a sum greater than 20
    7 and 25.6 have a sum greater than 20
    9 and 13 have a sum greater than 20
    13 and 13 have a sum greater than 20
    */
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.