Help with List<>..Find()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?UGhpbA==?=

    Help with List<>..Find()

    I have seen the samples for Find that explain how to use the predicate, but
    they are always searching for a pre-defined value. What I don't understand
    is how to search for a random value stored in a variable.

    For example,

    List<intitems;
    int val=3;

    items.Add(1);
    items.Add(2);
    items.Add(3);
    items.Add(4);

    How do I search the list "items" for the value in the variable val?

    Thanks,

    Phil
  • zacks@construction-imaging.com

    #2
    Re: Help with List&lt;&gt;..F ind()

    On May 7, 9:17 am, Phil <phi...@sbcglob al.netwrote:
    I have seen the samples for Find that explain how to use the predicate, but
    they are always searching for a pre-defined value.  What I don't understand
    is how to search for a random value stored in a variable.
    >
    For example,
    >
    List<intitems;
    int val=3;
    >
    items.Add(1);
    items.Add(2);
    items.Add(3);
    items.Add(4);
    >
    How do I search the list "items" for the value in the variable val?
    >
    Thanks,
    >
    Phil
    One way is to use the .Contains method, but it will only find the
    first one. There is also .FindExactStrin g method if the list is a list
    of strings.

    Comment

    • parez

      #3
      Re: Help with List&lt;&gt;..F ind()

      On May 7, 9:17 am, Phil <phi...@sbcglob al.netwrote:
      I have seen the samples for Find that explain how to use the predicate, but
      they are always searching for a pre-defined value. What I don't understand
      is how to search for a random value stored in a variable.
      >
      For example,
      >
      List<intitems;
      int val=3;
      >
      items.Add(1);
      items.Add(2);
      items.Add(3);
      items.Add(4);
      >
      How do I search the list "items" for the value in the variable val?
      >
      Thanks,
      >
      Phil
      int b = items.Find(dele gate(int a) { return a == val; });

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Help with List&lt;&gt;..F ind()

        On May 7, 2:26 pm, parez <psaw...@gmail. comwrote:
        int b = items.Find(dele gate(int a) { return a == val; });
        Or in C# 3:

        int b = items.Find(a =a==val);

        Jon

        Comment

        • sloan

          #5
          Re: Help with List&lt;&gt;..F ind()


          If you're only concerned with int's..you have an answer already.

          If you're interested in this .. in general..I'd suggest:
          http://ludwig-stuyck.spaces.l ive.com/Lists/cns!E36D9BA98FC 913B3!296/

          Find the C# Generics article (in english) and bookmark and read that.

          ...

          "Phil" <phil78@sbcglob al.netwrote in message
          news:B1EE89D2-7DD5-41AF-8A90-EB4D98E02510@mi crosoft.com...
          >I have seen the samples for Find that explain how to use the predicate, but
          they are always searching for a pre-defined value. What I don't
          understand
          is how to search for a random value stored in a variable.
          >
          For example,
          >
          List<intitems;
          int val=3;
          >
          items.Add(1);
          items.Add(2);
          items.Add(3);
          items.Add(4);
          >
          How do I search the list "items" for the value in the variable val?
          >
          Thanks,
          >
          Phil

          Comment

          • Leo Seccia

            #6
            Re: Help with List&lt;&gt;..F ind()

            Try:
            items.Select(i =i == val);
            or
            items.Find(i =i == val);
            or
            items.Where(i =i == val);

            depending on what you mean by Find... refer to


            Regards,
            Leo

            "Phil" <phil78@sbcglob al.netwrote in message
            news:B1EE89D2-7DD5-41AF-8A90-EB4D98E02510@mi crosoft.com...
            >I have seen the samples for Find that explain how to use the predicate, but
            they are always searching for a pre-defined value. What I don't
            understand
            is how to search for a random value stored in a variable.
            >
            For example,
            >
            List<intitems;
            int val=3;
            >
            items.Add(1);
            items.Add(2);
            items.Add(3);
            items.Add(4);
            >
            How do I search the list "items" for the value in the variable val?
            >
            Thanks,
            >
            Phil

            Comment

            • Ben Voigt [C++ MVP]

              #7
              Re: Help with List&lt;&gt;..F ind()

              Phil wrote:
              I have seen the samples for Find that explain how to use the
              predicate, but they are always searching for a pre-defined value.
              What I don't understand is how to search for a random value stored in
              a variable.
              Are you sure you don't want IndexOf instead of Find?


              Comment

              Working...