16

I have a list of Tuples I am trying to run a Select and Where query on to return a list of Objects from the Tuple.Item5 parameter. In my where clause I am looking to match Tuple.Item4 to a local variable.

I'm not sure what the VB.NET syntax is for the Select portion, I only know the c# syntax.

Essentially I am trying to select Tuple.Item5 from my list of tuples where Tuple.Item4 = sCurID. I'm unsure as to what should go in the Select section although in c# I believe it would be Select(t => t.Item5)

This is what I have:

listObj = listTuples.Select( Unsure What Goes Here ).Where(Function(w) w.Item4 = sCurID)
2
  • The same you already use for the Where part
    – Steve
    Aug 2, 2017 at 17:43
  • @Steve Not the same. Where takes a function that, given a sequence element, returns a Boolean - acts as a filter. Select takes a function that, given a sequence element, returns a related object - the object to be collected in the result sequence. These are different return types, and purposes. Oct 12, 2019 at 9:15

2 Answers 2

40

Once you apply the Select in C# or VB, you have reduced the Tuple to the Item5 value and can't access Item4. Do the Select last:

Dim listObj = listTuples.Where(Function(t) t.Item4 = sCurId).Select(Function(t) t.Item5)
-2
listObj = listTuples.Select(Function(t) t.Item5).Where(Function(w) w.Item4 = sCurID).ToList()
2
  • 1
    The issue is that when I use this my w variable is an Object and not a Tuple, which I'm trying to filter on. It only has the properties of my t.Item5 returned in the select section and not the properties of a tuple item. Aug 2, 2017 at 17:51
  • It has t.item5 because you're telling it to. If you want the entire Tuple, you should be fine with just "...Select(Function(t) t)
    – Aaron
    Aug 2, 2017 at 18:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.