0

Please consider the following list of ValueTuple C#7

static void Main(string[] args)

{

 List<(double prices, int matches)> myList = new List<(double, int)>();

                myList.Add((100, 10));  

                myList.Add((100.50 , 12)); 

                myList.Add((101 , 14));

                myList.Add((101.50 , 16));

}

What would be a simple way to search for items meeting conditions for "prices" AND "matches" within the List and return the results in another List of ValueTuple.

For instance if I want to return another List of ValueTuples meeting "prices > 101 and matches > 6"

Can you post an example please?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Roblogic
  • 107
  • 3
  • 8

1 Answers1

1

It's easier if you give names to the items :

var myList = new List<(double d,int i)>
             {
                 (100, 10),
                 (100.50 , 12),
                 (101 , 14),
                 (101.50 , 16)
             };

var results = myList.Where(x => x.d>101 && x.i>6);

Without the names you'd have to write

var results = myList.Where(x => x.Item1>101 && x.Item2>6);

C# 7.3 added tuple equality but not comparison. You can now write :

var result = myList.Where(d=>d == (101,14));

and

var result = myList.Where(d=>d != (101,14));

But not

var result = myList.Where(d=>d > (101,14));
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236