简体   繁体   中英

How can I use LINQ to add a Where clause to a Tuple to return a single value in C#?

I have a Tuple defined like so:

List<Tuple<string, string, double>> myList

Now I want to extract the 'double' value when string1 matches a certain value and string 2 matches a certain value. I tried something like this, but it didn't work. Any tips are appreciated.

myList.Select(t => t.Item3).Where(t => t.Item1 = "test" && t.Item2 = "query");

This treats 't' as a double, and complains that double does not have an Item1 property.

You've got the order of Where and Select backwards:

myList.Where(t => t.Item1 == "test" && t.Item2 == "query")
      .Select(t => t.Item3);

Select transforms the item. In this case, on the left side going in you have an enumeration of Tuple<string, string, double> and the coming out on the right side is an enumeration of double .

Additionally, you've got = where you mean == ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM