0

I have a variable of type List<Detail>

public class Detail {
   public string L { get; set; }
   public string R { get; set; }
}

I also have the value of a string that matches the value in Detail.L.

Is there an easy way that I can get the value of Detail.R that matches this?

Marie
  • 3
  • 1
  • do you want to search in list details for a specified string ? – Abhishek Jul 28 '11 at 07:48
  • Any reason why SortedDictionary wouldn't work? Or even SortedDictionary . – Skizz Jul 28 '11 at 07:49
  • You have a string that matches Detail.L and are searching for a way to find the value of Detail.R that matches this ? Isn´t the value always the same in this case ? – Destructor Jul 28 '11 at 07:50

8 Answers8

2

Sure, using LINQ to Objects (assuming you're using .NET 3.5 or higher):

string searchText = "the string to look for";
var matchingR = details.First(d => d.L == searchText).R;

Obviously that will find the first match. If you want to get all matches, you can do:

var matchingRs = details.Where(d => d.L == searchText)
                        .Select(d => d.R);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

If L is unique as it seems in your case, it would be better to use a Dictionary here instead of a List.

var myList = new Dictionary<string,string>();

Then use L as the key and R as the value for the dictionary.

Add entries by calling myList.Add(newL,newR);

Then get a entry by doing this:

string myR = myList[myL];
Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
  • 2
    That makes sense if you're going to query it multiple times, and don't need the ordering for anything else. If you need order though, and don't mind an O(N) search (e.g. you're searching relatively rarely) then it's fine to be in a List. – Jon Skeet Jul 28 '11 at 07:50
1
   string valueForL = "abc";
   List<Detail> details = new List<Detail>();
   string valueForR = (from d in details where d.L == valueForL select d.R).FirstOrDefault();
Dave
  • 3,581
  • 1
  • 22
  • 25
1

Like this to get all matching lefts:

IEnumerable<string> matchRightsToLeft(List<Detail> list, string left)
{
    return list.Where(l => l.left == left).Select(l => l.right);
}

or for the first/only match

string matchRightToLeft(List<Detail> list, string left)
{
    return list.Where(l => l.left == left).FirstOrDefault(l => l.right);
}
Mark Holland
  • 846
  • 4
  • 8
  • 1
    Your second one is wrong - the function taken by `FirstOrDefault` is a predicate, not a projection. I'd also suggest following .NET naming conventions :) – Jon Skeet Jul 28 '11 at 08:28
  • Thanks Jon. I could argue mitigating circumstances that I was doing it off of the top of my head but then I know the same could be said for you too. ;-) – Mark Holland Jul 28 '11 at 09:29
0

take a look at this thread about implementing the IEquatable interface. I guess you'll find all you need there.

Community
  • 1
  • 1
Timothée Bourguignon
  • 2,190
  • 3
  • 23
  • 39
0

How about:

IEnumerable<string> allR = Details.Where(d => Equals(d.L, otherString)).Select(d => d.R);
Arcturus
  • 26,677
  • 10
  • 92
  • 107
0

A for-loop would be the basic way to do it.

Otherwise use Linq:

var lString = SearchValue;
DetailList.Where(o => o.R == lString) //This will give you a list of all the Detail object where R == lString.
Andre Luus
  • 3,692
  • 3
  • 33
  • 46
0

You might try LINQ to Objects:

string r = (from d in list where d.L.Equals(...) select d.R).FirstOrDefault();
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139