Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I have a tuple list which I want to extract the item2 value for if base on a search on item1.

Depending on the work location the results might not always have the same items.

Eg

Work1
Item1 = HairStylistID Item2 = 01235
Item1 NailTechID Item2 = 1452

Work2
Item1 = HairStylistID Item2 = 08975

I want to be able to store these values in variables but even when I set my fields to nullable if it can find a match in the tuple list it crashes.

Any help would be really appreciated
Thanks

What I have tried:

C#
Guid? NailTechID = list.Select(v => v.Item2).FirstOrDefault((t) => t.Item1 == "NailTechID");


Guid? NailTechID = list.Select(v => v.Item2).FirstOrDefault((t) => t.Item1 == "NailTechID") ?? new Guid("");
Posted
Updated 3-Jun-20 6:41am
Comments
F-ES Sitecore 3-Jun-20 12:56pm    
In addition to Richard's solution about filtering on Item1 first and then selecting Item2, you are expecting Item2 to be a GUID but in your data you're not using GUIDs as the second item type. If the sample data you have is accurate it would help if you added the type definition of "list"

1 solution

You need to filter before you project:
C#
var NailTechID = list.Where(v => v.Item1 == "NailTechID").Select(v => v.Item2).FirstOrDefault();
 
Share this answer
 
v2
Comments
F-ES Sitecore 3-Jun-20 12:53pm    
Minor nit-pick but

var NailTechID = list.Where(v => v.Item1 == "NailTechID").Select(v => v.Item2).FirstOrDefault();
Richard Deeming 3-Jun-20 12:54pm    
D'Oh! That's what I meant. Fixed.
Maciej Los 3-Jun-20 14:32pm    
Sorry, but not fixed ;(
Sorry, my bad ;)
5ed!
George Swan 3-Jun-20 14:27pm    
Could I suggest that you use named tuples so that you can avoid referring to Item1,Item2 as that's error prone? Something like this:
List<(int Id, string Name)> tuples = new List<(int, string)> { (1, "A"), (2, "B"), (1, "C") };
var id=tuples.Where(v=> v.Id == 1).Select(v => v.Id).FirstOrDefault();
George Swan 3-Jun-20 14:31pm    
Sorry, I should have made it clear that my previous comment was addressed to the op, not Richard

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900