2

I'm new to programming, and I created a tuple list with

var tupleList = new List<Tuple<string, string>> { };

Later on in the code, I'd like to add an element like

tupleList.Add(string1,string2);

but .Add doesn't support this somehow?

Basically, I'm going through a loop and adding to the tuple and later I want to search through the tuple for a sample string, so my second question is how would I search through tupleList.Item1 and get all the pairs that equal, for example string10? I saw an answer for dictionary values, but can I do the same for tuples?

var matches = tupleList.Where(pair => pair.Item1.Equals(string10))
                  .Select(pair => Item2.Key);

I don't know if that makes sense though, this was the original code:

var matches = dict.Where(pair => pair.Value == "abc")
                  .Select(pair => pair.Key);
2
  • I rolled back your edit. If you have another problems, then accept current question and ask new one. Don't modify question by adding new problems to it Commented Jun 29, 2014 at 21:23
  • Oh, ok, sorry, thanks. Commented Jun 29, 2014 at 21:34

2 Answers 2

5

List<T> does not have any specific methods for working with tuples. It works with any type T. If you want to add new item to list, you should create item of list's type T and pass it to list. Adding new tuple:

tupleList.Add(Tuple.Create(string1,string2));

For searching just filter tuples list. You should not project tuples with Select operator if you want to get them as result:

var matches = tupleList.Where(pair => pair.Item1 == string10);

NOTE: I don't like tuples for their meaningless names Item1, Item2 etc, which is hard to understand. Consider creating custom class which will have properties with descriptive names.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks, adding tuples with your code made the dreaded red squiggly go away. As for the matches, I realized that what I really want is any string that has a match, so instead of doing a foreach and going through every Item1 and seeing if it has a match elsewhere, could I just delete any tuples (with respect to Item1) that don't appear twice or more?
@user3408097 it's not very clear what you are trying to achieve. You want to select only first tuple which matches given Item1 value?
Oh, sorry, what I meant was, for example, if I have the following list [(1,2)(1,4)(1,5)(2,3)(4,6)(1,3)(3,7)] I just want to remove (2,3) because 2 only exists once. I want to KEEP the duplicates based on the Item1 value
@user3408097 still not clear - you have (4,6) and (3,7) which exist only once. Why only (2,3) should be removed?
Actually you answered all the questions perfectly, it was just a dumb implementation mistake by me (just got into programming recently).
|
0

I'd say why do the same for tuples?

If you are using a tuple to represent a key value pair, just stick to a key value pair, which is what a dictionary contains a collection of. If you model a row with more than 2 values, I'd probably favour a strongly typed model over this, where you can be more explicit in your LINQ queries.

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.