Go through custom type list and return the first object where a certain member starts with letter “x”











up vote
1
down vote

favorite












this should be easy but I havent quite figured it out yet:



I have a list of my custom type Mp3ObjectSmall.
This is basically a list containing mp3s with the URI on the phone, the artist and the song name. This is the type:



public class MP3objectSmall
{
public string SongName { get; set; }
public string ArtistName { get; set; }
public string Mp3Uri { get; set; }
}


This list contains about 700 items, each with a different song.



Now, for my recycler view, I need to be able to for instance click on the letter "x" in my app and then have a function return an int with first item in above list containing an "x" as a song name.



Even if I had say 100 song names that all begin with the letter "x" I need to just be returned the first item's position.



How do I find the first string of a member of a custom type list that starts with a certain letter?



If anythin is unclear, just comment on it!



Thank you :-)



What I have tried so far:



        if (letter != "0") // scroll to top when it is 0, so nothing needs to be done here 
{
foreach(MP3objectSmall o in Mp3ObjectSmall)
{
if (letter.Equals(o.SongName.StartsWith(letter)))
{

}

}
}









share|improve this question




















  • 3




    What have you tried so far?
    – Patrick Hofman
    Nov 19 at 11:17










  • Thank you, but running a company does not neccissarly mean I have to be good at coding.
    – Innomotion Media
    Nov 19 at 11:20










  • So why doesn't that code work? What does it do, and what not?
    – Patrick Hofman
    Nov 19 at 11:22










  • Well, it crashes saying that "o" is not set be an instance of reference
    – Innomotion Media
    Nov 19 at 11:24






  • 1




    That suggests your collection contains a null reference.
    – Jon Skeet
    Nov 19 at 11:30















up vote
1
down vote

favorite












this should be easy but I havent quite figured it out yet:



I have a list of my custom type Mp3ObjectSmall.
This is basically a list containing mp3s with the URI on the phone, the artist and the song name. This is the type:



public class MP3objectSmall
{
public string SongName { get; set; }
public string ArtistName { get; set; }
public string Mp3Uri { get; set; }
}


This list contains about 700 items, each with a different song.



Now, for my recycler view, I need to be able to for instance click on the letter "x" in my app and then have a function return an int with first item in above list containing an "x" as a song name.



Even if I had say 100 song names that all begin with the letter "x" I need to just be returned the first item's position.



How do I find the first string of a member of a custom type list that starts with a certain letter?



If anythin is unclear, just comment on it!



Thank you :-)



What I have tried so far:



        if (letter != "0") // scroll to top when it is 0, so nothing needs to be done here 
{
foreach(MP3objectSmall o in Mp3ObjectSmall)
{
if (letter.Equals(o.SongName.StartsWith(letter)))
{

}

}
}









share|improve this question




















  • 3




    What have you tried so far?
    – Patrick Hofman
    Nov 19 at 11:17










  • Thank you, but running a company does not neccissarly mean I have to be good at coding.
    – Innomotion Media
    Nov 19 at 11:20










  • So why doesn't that code work? What does it do, and what not?
    – Patrick Hofman
    Nov 19 at 11:22










  • Well, it crashes saying that "o" is not set be an instance of reference
    – Innomotion Media
    Nov 19 at 11:24






  • 1




    That suggests your collection contains a null reference.
    – Jon Skeet
    Nov 19 at 11:30













up vote
1
down vote

favorite









up vote
1
down vote

favorite











this should be easy but I havent quite figured it out yet:



I have a list of my custom type Mp3ObjectSmall.
This is basically a list containing mp3s with the URI on the phone, the artist and the song name. This is the type:



public class MP3objectSmall
{
public string SongName { get; set; }
public string ArtistName { get; set; }
public string Mp3Uri { get; set; }
}


This list contains about 700 items, each with a different song.



Now, for my recycler view, I need to be able to for instance click on the letter "x" in my app and then have a function return an int with first item in above list containing an "x" as a song name.



Even if I had say 100 song names that all begin with the letter "x" I need to just be returned the first item's position.



How do I find the first string of a member of a custom type list that starts with a certain letter?



If anythin is unclear, just comment on it!



Thank you :-)



What I have tried so far:



        if (letter != "0") // scroll to top when it is 0, so nothing needs to be done here 
{
foreach(MP3objectSmall o in Mp3ObjectSmall)
{
if (letter.Equals(o.SongName.StartsWith(letter)))
{

}

}
}









share|improve this question















this should be easy but I havent quite figured it out yet:



I have a list of my custom type Mp3ObjectSmall.
This is basically a list containing mp3s with the URI on the phone, the artist and the song name. This is the type:



public class MP3objectSmall
{
public string SongName { get; set; }
public string ArtistName { get; set; }
public string Mp3Uri { get; set; }
}


This list contains about 700 items, each with a different song.



Now, for my recycler view, I need to be able to for instance click on the letter "x" in my app and then have a function return an int with first item in above list containing an "x" as a song name.



Even if I had say 100 song names that all begin with the letter "x" I need to just be returned the first item's position.



How do I find the first string of a member of a custom type list that starts with a certain letter?



If anythin is unclear, just comment on it!



Thank you :-)



What I have tried so far:



        if (letter != "0") // scroll to top when it is 0, so nothing needs to be done here 
{
foreach(MP3objectSmall o in Mp3ObjectSmall)
{
if (letter.Equals(o.SongName.StartsWith(letter)))
{

}

}
}






c# list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 11:20

























asked Nov 19 at 11:16









Innomotion Media

266




266








  • 3




    What have you tried so far?
    – Patrick Hofman
    Nov 19 at 11:17










  • Thank you, but running a company does not neccissarly mean I have to be good at coding.
    – Innomotion Media
    Nov 19 at 11:20










  • So why doesn't that code work? What does it do, and what not?
    – Patrick Hofman
    Nov 19 at 11:22










  • Well, it crashes saying that "o" is not set be an instance of reference
    – Innomotion Media
    Nov 19 at 11:24






  • 1




    That suggests your collection contains a null reference.
    – Jon Skeet
    Nov 19 at 11:30














  • 3




    What have you tried so far?
    – Patrick Hofman
    Nov 19 at 11:17










  • Thank you, but running a company does not neccissarly mean I have to be good at coding.
    – Innomotion Media
    Nov 19 at 11:20










  • So why doesn't that code work? What does it do, and what not?
    – Patrick Hofman
    Nov 19 at 11:22










  • Well, it crashes saying that "o" is not set be an instance of reference
    – Innomotion Media
    Nov 19 at 11:24






  • 1




    That suggests your collection contains a null reference.
    – Jon Skeet
    Nov 19 at 11:30








3




3




What have you tried so far?
– Patrick Hofman
Nov 19 at 11:17




What have you tried so far?
– Patrick Hofman
Nov 19 at 11:17












Thank you, but running a company does not neccissarly mean I have to be good at coding.
– Innomotion Media
Nov 19 at 11:20




Thank you, but running a company does not neccissarly mean I have to be good at coding.
– Innomotion Media
Nov 19 at 11:20












So why doesn't that code work? What does it do, and what not?
– Patrick Hofman
Nov 19 at 11:22




So why doesn't that code work? What does it do, and what not?
– Patrick Hofman
Nov 19 at 11:22












Well, it crashes saying that "o" is not set be an instance of reference
– Innomotion Media
Nov 19 at 11:24




Well, it crashes saying that "o" is not set be an instance of reference
– Innomotion Media
Nov 19 at 11:24




1




1




That suggests your collection contains a null reference.
– Jon Skeet
Nov 19 at 11:30




That suggests your collection contains a null reference.
– Jon Skeet
Nov 19 at 11:30












4 Answers
4






active

oldest

votes

















up vote
1
down vote



accepted










Use Linq. The simple example for your case.



Also, check Lambda Expressions



var list = new List<MP3objectSmall>();
var s1 = new MP3objectSmall { SongName = "abc" };
var s2 = new MP3objectSmall { SongName = "def" };
var s3 = new MP3objectSmall { SongName = "xyz" };
var s4 = new MP3objectSmall { SongName = "xxx" };

list.Add(s1);
list.Add(s2);
list.Add(s3);
list.Add(s4);

var filteredList = list.Where(i => i.SongName.StartsWith("x")).ToList();

foreach(var item in filteredList)
{
Console.WriteLine(item.SongName);
}




  • list.Where will filter the list based on the provided condition


  • i => i.SongName is a lambda expression which allows accessing the object properties.


  • StartsWithprovides a way to find/filter the string which starts with specifi character


Remember to import these namespaces



using System.Collections.Generic;
using System.Linq;


Also, use FirstOrDefault as the question is to fetch the first element after filters.






share|improve this answer























  • Hi and thank you. Unfortunately, no matter what letter i put, the list always returns 0 objects
    – Innomotion Media
    Nov 19 at 11:28










  • Could you please update the code with some sample source you're using?
    – mbharanidharan88
    Nov 19 at 11:30






  • 1




    sorry. you also are right. I confused upper with lowercase letters! :)
    – Innomotion Media
    Nov 19 at 11:33










  • i => i.SongName.toLowerCase().StartsWith("x") - no matter what case and you can provide the filter criteria with lower case
    – mbharanidharan88
    Nov 19 at 11:34






  • 1




    The filteredList will hold a List<MP3objectSmall> containing both s3 and s4 - but that was not what was asked for in the question... If you're going to use linq, I think you should just use FirstOrDefault with the predicate you have. This will return only the first occurrence or default(MP3objectSmall) (which is null) if not found.
    – Zohar Peled
    Nov 19 at 11:45


















up vote
1
down vote













I think you are looking for FindIndex:



var indexOfFirstSongThatStartsWithX = Mp3ObjectSmall
.FindIndex(s => s.SongName.StartsWith("x"));


This will return the index of the first item in the list where the SongName property starts with an x.



If you want the item itself, you can use Find the same way:



var firstSongThatStartsWithX = Mp3ObjectSmall
.Find(s => s.SongName.StartsWith("x"));


This will return the first item where the SongName property starts with an x.






share|improve this answer






























    up vote
    0
    down vote













    Another solution: https://dotnetfiddle.net/RzH5li



    public static void Main()
    {

    List<MP3objectSmall> songs = new List<MP3objectSmall>();

    songs.Add(new MP3objectSmall(){SongName ="a"});
    songs.Add(new MP3objectSmall(){SongName ="b"});
    songs.Add(new MP3objectSmall(){SongName ="x"});
    songs.Add(new MP3objectSmall(){SongName ="z"});

    string letter = "x";

    MP3objectSmall firstSong = songs.FirstOrDefault(x => x.SongName.ToLowerInvariant().StartsWith(letter));

    if(firstSong != null)
    {
    int index = songs.IndexOf(firstSong);
    Console.WriteLine(index);
    }
    }





    share|improve this answer




























      up vote
      0
      down vote













      I think you are missing the List<> variable itself.



      For example, assumed you have a similar code:



      var songList = /* some function returning a List object like: List<MP3objectSmall> */;


      You could then do some List filtering:



      int desiredIndex = songList.FindIndex(file => file.SongName.StartWith(letter));


      Your index is then in the variable desiredIndex






      share|improve this answer























      • FindIndex is a method of List, there's no linq involved there.
        – Zohar Peled
        Nov 19 at 11:30












      • thank you. however, this returns -1 ...
        – Innomotion Media
        Nov 19 at 11:31






      • 1




        Oh sorry, this totally worked. There was just confusion with upper and lowercase letters. thanks a lot!
        – Innomotion Media
        Nov 19 at 11:33










      • @ZoharPeled totally right. At first I had some Linq expression there. Forgot to adjust the text. Edited now!
        – r3dst0rm
        Nov 19 at 11:44











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373469%2fgo-through-custom-type-list-and-return-the-first-object-where-a-certain-member-s%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      Use Linq. The simple example for your case.



      Also, check Lambda Expressions



      var list = new List<MP3objectSmall>();
      var s1 = new MP3objectSmall { SongName = "abc" };
      var s2 = new MP3objectSmall { SongName = "def" };
      var s3 = new MP3objectSmall { SongName = "xyz" };
      var s4 = new MP3objectSmall { SongName = "xxx" };

      list.Add(s1);
      list.Add(s2);
      list.Add(s3);
      list.Add(s4);

      var filteredList = list.Where(i => i.SongName.StartsWith("x")).ToList();

      foreach(var item in filteredList)
      {
      Console.WriteLine(item.SongName);
      }




      • list.Where will filter the list based on the provided condition


      • i => i.SongName is a lambda expression which allows accessing the object properties.


      • StartsWithprovides a way to find/filter the string which starts with specifi character


      Remember to import these namespaces



      using System.Collections.Generic;
      using System.Linq;


      Also, use FirstOrDefault as the question is to fetch the first element after filters.






      share|improve this answer























      • Hi and thank you. Unfortunately, no matter what letter i put, the list always returns 0 objects
        – Innomotion Media
        Nov 19 at 11:28










      • Could you please update the code with some sample source you're using?
        – mbharanidharan88
        Nov 19 at 11:30






      • 1




        sorry. you also are right. I confused upper with lowercase letters! :)
        – Innomotion Media
        Nov 19 at 11:33










      • i => i.SongName.toLowerCase().StartsWith("x") - no matter what case and you can provide the filter criteria with lower case
        – mbharanidharan88
        Nov 19 at 11:34






      • 1




        The filteredList will hold a List<MP3objectSmall> containing both s3 and s4 - but that was not what was asked for in the question... If you're going to use linq, I think you should just use FirstOrDefault with the predicate you have. This will return only the first occurrence or default(MP3objectSmall) (which is null) if not found.
        – Zohar Peled
        Nov 19 at 11:45















      up vote
      1
      down vote



      accepted










      Use Linq. The simple example for your case.



      Also, check Lambda Expressions



      var list = new List<MP3objectSmall>();
      var s1 = new MP3objectSmall { SongName = "abc" };
      var s2 = new MP3objectSmall { SongName = "def" };
      var s3 = new MP3objectSmall { SongName = "xyz" };
      var s4 = new MP3objectSmall { SongName = "xxx" };

      list.Add(s1);
      list.Add(s2);
      list.Add(s3);
      list.Add(s4);

      var filteredList = list.Where(i => i.SongName.StartsWith("x")).ToList();

      foreach(var item in filteredList)
      {
      Console.WriteLine(item.SongName);
      }




      • list.Where will filter the list based on the provided condition


      • i => i.SongName is a lambda expression which allows accessing the object properties.


      • StartsWithprovides a way to find/filter the string which starts with specifi character


      Remember to import these namespaces



      using System.Collections.Generic;
      using System.Linq;


      Also, use FirstOrDefault as the question is to fetch the first element after filters.






      share|improve this answer























      • Hi and thank you. Unfortunately, no matter what letter i put, the list always returns 0 objects
        – Innomotion Media
        Nov 19 at 11:28










      • Could you please update the code with some sample source you're using?
        – mbharanidharan88
        Nov 19 at 11:30






      • 1




        sorry. you also are right. I confused upper with lowercase letters! :)
        – Innomotion Media
        Nov 19 at 11:33










      • i => i.SongName.toLowerCase().StartsWith("x") - no matter what case and you can provide the filter criteria with lower case
        – mbharanidharan88
        Nov 19 at 11:34






      • 1




        The filteredList will hold a List<MP3objectSmall> containing both s3 and s4 - but that was not what was asked for in the question... If you're going to use linq, I think you should just use FirstOrDefault with the predicate you have. This will return only the first occurrence or default(MP3objectSmall) (which is null) if not found.
        – Zohar Peled
        Nov 19 at 11:45













      up vote
      1
      down vote



      accepted







      up vote
      1
      down vote



      accepted






      Use Linq. The simple example for your case.



      Also, check Lambda Expressions



      var list = new List<MP3objectSmall>();
      var s1 = new MP3objectSmall { SongName = "abc" };
      var s2 = new MP3objectSmall { SongName = "def" };
      var s3 = new MP3objectSmall { SongName = "xyz" };
      var s4 = new MP3objectSmall { SongName = "xxx" };

      list.Add(s1);
      list.Add(s2);
      list.Add(s3);
      list.Add(s4);

      var filteredList = list.Where(i => i.SongName.StartsWith("x")).ToList();

      foreach(var item in filteredList)
      {
      Console.WriteLine(item.SongName);
      }




      • list.Where will filter the list based on the provided condition


      • i => i.SongName is a lambda expression which allows accessing the object properties.


      • StartsWithprovides a way to find/filter the string which starts with specifi character


      Remember to import these namespaces



      using System.Collections.Generic;
      using System.Linq;


      Also, use FirstOrDefault as the question is to fetch the first element after filters.






      share|improve this answer














      Use Linq. The simple example for your case.



      Also, check Lambda Expressions



      var list = new List<MP3objectSmall>();
      var s1 = new MP3objectSmall { SongName = "abc" };
      var s2 = new MP3objectSmall { SongName = "def" };
      var s3 = new MP3objectSmall { SongName = "xyz" };
      var s4 = new MP3objectSmall { SongName = "xxx" };

      list.Add(s1);
      list.Add(s2);
      list.Add(s3);
      list.Add(s4);

      var filteredList = list.Where(i => i.SongName.StartsWith("x")).ToList();

      foreach(var item in filteredList)
      {
      Console.WriteLine(item.SongName);
      }




      • list.Where will filter the list based on the provided condition


      • i => i.SongName is a lambda expression which allows accessing the object properties.


      • StartsWithprovides a way to find/filter the string which starts with specifi character


      Remember to import these namespaces



      using System.Collections.Generic;
      using System.Linq;


      Also, use FirstOrDefault as the question is to fetch the first element after filters.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 19 at 12:14

























      answered Nov 19 at 11:24









      mbharanidharan88

      4,02032253




      4,02032253












      • Hi and thank you. Unfortunately, no matter what letter i put, the list always returns 0 objects
        – Innomotion Media
        Nov 19 at 11:28










      • Could you please update the code with some sample source you're using?
        – mbharanidharan88
        Nov 19 at 11:30






      • 1




        sorry. you also are right. I confused upper with lowercase letters! :)
        – Innomotion Media
        Nov 19 at 11:33










      • i => i.SongName.toLowerCase().StartsWith("x") - no matter what case and you can provide the filter criteria with lower case
        – mbharanidharan88
        Nov 19 at 11:34






      • 1




        The filteredList will hold a List<MP3objectSmall> containing both s3 and s4 - but that was not what was asked for in the question... If you're going to use linq, I think you should just use FirstOrDefault with the predicate you have. This will return only the first occurrence or default(MP3objectSmall) (which is null) if not found.
        – Zohar Peled
        Nov 19 at 11:45


















      • Hi and thank you. Unfortunately, no matter what letter i put, the list always returns 0 objects
        – Innomotion Media
        Nov 19 at 11:28










      • Could you please update the code with some sample source you're using?
        – mbharanidharan88
        Nov 19 at 11:30






      • 1




        sorry. you also are right. I confused upper with lowercase letters! :)
        – Innomotion Media
        Nov 19 at 11:33










      • i => i.SongName.toLowerCase().StartsWith("x") - no matter what case and you can provide the filter criteria with lower case
        – mbharanidharan88
        Nov 19 at 11:34






      • 1




        The filteredList will hold a List<MP3objectSmall> containing both s3 and s4 - but that was not what was asked for in the question... If you're going to use linq, I think you should just use FirstOrDefault with the predicate you have. This will return only the first occurrence or default(MP3objectSmall) (which is null) if not found.
        – Zohar Peled
        Nov 19 at 11:45
















      Hi and thank you. Unfortunately, no matter what letter i put, the list always returns 0 objects
      – Innomotion Media
      Nov 19 at 11:28




      Hi and thank you. Unfortunately, no matter what letter i put, the list always returns 0 objects
      – Innomotion Media
      Nov 19 at 11:28












      Could you please update the code with some sample source you're using?
      – mbharanidharan88
      Nov 19 at 11:30




      Could you please update the code with some sample source you're using?
      – mbharanidharan88
      Nov 19 at 11:30




      1




      1




      sorry. you also are right. I confused upper with lowercase letters! :)
      – Innomotion Media
      Nov 19 at 11:33




      sorry. you also are right. I confused upper with lowercase letters! :)
      – Innomotion Media
      Nov 19 at 11:33












      i => i.SongName.toLowerCase().StartsWith("x") - no matter what case and you can provide the filter criteria with lower case
      – mbharanidharan88
      Nov 19 at 11:34




      i => i.SongName.toLowerCase().StartsWith("x") - no matter what case and you can provide the filter criteria with lower case
      – mbharanidharan88
      Nov 19 at 11:34




      1




      1




      The filteredList will hold a List<MP3objectSmall> containing both s3 and s4 - but that was not what was asked for in the question... If you're going to use linq, I think you should just use FirstOrDefault with the predicate you have. This will return only the first occurrence or default(MP3objectSmall) (which is null) if not found.
      – Zohar Peled
      Nov 19 at 11:45




      The filteredList will hold a List<MP3objectSmall> containing both s3 and s4 - but that was not what was asked for in the question... If you're going to use linq, I think you should just use FirstOrDefault with the predicate you have. This will return only the first occurrence or default(MP3objectSmall) (which is null) if not found.
      – Zohar Peled
      Nov 19 at 11:45












      up vote
      1
      down vote













      I think you are looking for FindIndex:



      var indexOfFirstSongThatStartsWithX = Mp3ObjectSmall
      .FindIndex(s => s.SongName.StartsWith("x"));


      This will return the index of the first item in the list where the SongName property starts with an x.



      If you want the item itself, you can use Find the same way:



      var firstSongThatStartsWithX = Mp3ObjectSmall
      .Find(s => s.SongName.StartsWith("x"));


      This will return the first item where the SongName property starts with an x.






      share|improve this answer



























        up vote
        1
        down vote













        I think you are looking for FindIndex:



        var indexOfFirstSongThatStartsWithX = Mp3ObjectSmall
        .FindIndex(s => s.SongName.StartsWith("x"));


        This will return the index of the first item in the list where the SongName property starts with an x.



        If you want the item itself, you can use Find the same way:



        var firstSongThatStartsWithX = Mp3ObjectSmall
        .Find(s => s.SongName.StartsWith("x"));


        This will return the first item where the SongName property starts with an x.






        share|improve this answer

























          up vote
          1
          down vote










          up vote
          1
          down vote









          I think you are looking for FindIndex:



          var indexOfFirstSongThatStartsWithX = Mp3ObjectSmall
          .FindIndex(s => s.SongName.StartsWith("x"));


          This will return the index of the first item in the list where the SongName property starts with an x.



          If you want the item itself, you can use Find the same way:



          var firstSongThatStartsWithX = Mp3ObjectSmall
          .Find(s => s.SongName.StartsWith("x"));


          This will return the first item where the SongName property starts with an x.






          share|improve this answer














          I think you are looking for FindIndex:



          var indexOfFirstSongThatStartsWithX = Mp3ObjectSmall
          .FindIndex(s => s.SongName.StartsWith("x"));


          This will return the index of the first item in the list where the SongName property starts with an x.



          If you want the item itself, you can use Find the same way:



          var firstSongThatStartsWithX = Mp3ObjectSmall
          .Find(s => s.SongName.StartsWith("x"));


          This will return the first item where the SongName property starts with an x.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 at 11:32

























          answered Nov 19 at 11:27









          Zohar Peled

          51.6k73172




          51.6k73172






















              up vote
              0
              down vote













              Another solution: https://dotnetfiddle.net/RzH5li



              public static void Main()
              {

              List<MP3objectSmall> songs = new List<MP3objectSmall>();

              songs.Add(new MP3objectSmall(){SongName ="a"});
              songs.Add(new MP3objectSmall(){SongName ="b"});
              songs.Add(new MP3objectSmall(){SongName ="x"});
              songs.Add(new MP3objectSmall(){SongName ="z"});

              string letter = "x";

              MP3objectSmall firstSong = songs.FirstOrDefault(x => x.SongName.ToLowerInvariant().StartsWith(letter));

              if(firstSong != null)
              {
              int index = songs.IndexOf(firstSong);
              Console.WriteLine(index);
              }
              }





              share|improve this answer

























                up vote
                0
                down vote













                Another solution: https://dotnetfiddle.net/RzH5li



                public static void Main()
                {

                List<MP3objectSmall> songs = new List<MP3objectSmall>();

                songs.Add(new MP3objectSmall(){SongName ="a"});
                songs.Add(new MP3objectSmall(){SongName ="b"});
                songs.Add(new MP3objectSmall(){SongName ="x"});
                songs.Add(new MP3objectSmall(){SongName ="z"});

                string letter = "x";

                MP3objectSmall firstSong = songs.FirstOrDefault(x => x.SongName.ToLowerInvariant().StartsWith(letter));

                if(firstSong != null)
                {
                int index = songs.IndexOf(firstSong);
                Console.WriteLine(index);
                }
                }





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Another solution: https://dotnetfiddle.net/RzH5li



                  public static void Main()
                  {

                  List<MP3objectSmall> songs = new List<MP3objectSmall>();

                  songs.Add(new MP3objectSmall(){SongName ="a"});
                  songs.Add(new MP3objectSmall(){SongName ="b"});
                  songs.Add(new MP3objectSmall(){SongName ="x"});
                  songs.Add(new MP3objectSmall(){SongName ="z"});

                  string letter = "x";

                  MP3objectSmall firstSong = songs.FirstOrDefault(x => x.SongName.ToLowerInvariant().StartsWith(letter));

                  if(firstSong != null)
                  {
                  int index = songs.IndexOf(firstSong);
                  Console.WriteLine(index);
                  }
                  }





                  share|improve this answer












                  Another solution: https://dotnetfiddle.net/RzH5li



                  public static void Main()
                  {

                  List<MP3objectSmall> songs = new List<MP3objectSmall>();

                  songs.Add(new MP3objectSmall(){SongName ="a"});
                  songs.Add(new MP3objectSmall(){SongName ="b"});
                  songs.Add(new MP3objectSmall(){SongName ="x"});
                  songs.Add(new MP3objectSmall(){SongName ="z"});

                  string letter = "x";

                  MP3objectSmall firstSong = songs.FirstOrDefault(x => x.SongName.ToLowerInvariant().StartsWith(letter));

                  if(firstSong != null)
                  {
                  int index = songs.IndexOf(firstSong);
                  Console.WriteLine(index);
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 at 11:42









                  Mat

                  1,25422029




                  1,25422029






















                      up vote
                      0
                      down vote













                      I think you are missing the List<> variable itself.



                      For example, assumed you have a similar code:



                      var songList = /* some function returning a List object like: List<MP3objectSmall> */;


                      You could then do some List filtering:



                      int desiredIndex = songList.FindIndex(file => file.SongName.StartWith(letter));


                      Your index is then in the variable desiredIndex






                      share|improve this answer























                      • FindIndex is a method of List, there's no linq involved there.
                        – Zohar Peled
                        Nov 19 at 11:30












                      • thank you. however, this returns -1 ...
                        – Innomotion Media
                        Nov 19 at 11:31






                      • 1




                        Oh sorry, this totally worked. There was just confusion with upper and lowercase letters. thanks a lot!
                        – Innomotion Media
                        Nov 19 at 11:33










                      • @ZoharPeled totally right. At first I had some Linq expression there. Forgot to adjust the text. Edited now!
                        – r3dst0rm
                        Nov 19 at 11:44















                      up vote
                      0
                      down vote













                      I think you are missing the List<> variable itself.



                      For example, assumed you have a similar code:



                      var songList = /* some function returning a List object like: List<MP3objectSmall> */;


                      You could then do some List filtering:



                      int desiredIndex = songList.FindIndex(file => file.SongName.StartWith(letter));


                      Your index is then in the variable desiredIndex






                      share|improve this answer























                      • FindIndex is a method of List, there's no linq involved there.
                        – Zohar Peled
                        Nov 19 at 11:30












                      • thank you. however, this returns -1 ...
                        – Innomotion Media
                        Nov 19 at 11:31






                      • 1




                        Oh sorry, this totally worked. There was just confusion with upper and lowercase letters. thanks a lot!
                        – Innomotion Media
                        Nov 19 at 11:33










                      • @ZoharPeled totally right. At first I had some Linq expression there. Forgot to adjust the text. Edited now!
                        – r3dst0rm
                        Nov 19 at 11:44













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      I think you are missing the List<> variable itself.



                      For example, assumed you have a similar code:



                      var songList = /* some function returning a List object like: List<MP3objectSmall> */;


                      You could then do some List filtering:



                      int desiredIndex = songList.FindIndex(file => file.SongName.StartWith(letter));


                      Your index is then in the variable desiredIndex






                      share|improve this answer














                      I think you are missing the List<> variable itself.



                      For example, assumed you have a similar code:



                      var songList = /* some function returning a List object like: List<MP3objectSmall> */;


                      You could then do some List filtering:



                      int desiredIndex = songList.FindIndex(file => file.SongName.StartWith(letter));


                      Your index is then in the variable desiredIndex







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 19 at 11:43

























                      answered Nov 19 at 11:26









                      r3dst0rm

                      602613




                      602613












                      • FindIndex is a method of List, there's no linq involved there.
                        – Zohar Peled
                        Nov 19 at 11:30












                      • thank you. however, this returns -1 ...
                        – Innomotion Media
                        Nov 19 at 11:31






                      • 1




                        Oh sorry, this totally worked. There was just confusion with upper and lowercase letters. thanks a lot!
                        – Innomotion Media
                        Nov 19 at 11:33










                      • @ZoharPeled totally right. At first I had some Linq expression there. Forgot to adjust the text. Edited now!
                        – r3dst0rm
                        Nov 19 at 11:44


















                      • FindIndex is a method of List, there's no linq involved there.
                        – Zohar Peled
                        Nov 19 at 11:30












                      • thank you. however, this returns -1 ...
                        – Innomotion Media
                        Nov 19 at 11:31






                      • 1




                        Oh sorry, this totally worked. There was just confusion with upper and lowercase letters. thanks a lot!
                        – Innomotion Media
                        Nov 19 at 11:33










                      • @ZoharPeled totally right. At first I had some Linq expression there. Forgot to adjust the text. Edited now!
                        – r3dst0rm
                        Nov 19 at 11:44
















                      FindIndex is a method of List, there's no linq involved there.
                      – Zohar Peled
                      Nov 19 at 11:30






                      FindIndex is a method of List, there's no linq involved there.
                      – Zohar Peled
                      Nov 19 at 11:30














                      thank you. however, this returns -1 ...
                      – Innomotion Media
                      Nov 19 at 11:31




                      thank you. however, this returns -1 ...
                      – Innomotion Media
                      Nov 19 at 11:31




                      1




                      1




                      Oh sorry, this totally worked. There was just confusion with upper and lowercase letters. thanks a lot!
                      – Innomotion Media
                      Nov 19 at 11:33




                      Oh sorry, this totally worked. There was just confusion with upper and lowercase letters. thanks a lot!
                      – Innomotion Media
                      Nov 19 at 11:33












                      @ZoharPeled totally right. At first I had some Linq expression there. Forgot to adjust the text. Edited now!
                      – r3dst0rm
                      Nov 19 at 11:44




                      @ZoharPeled totally right. At first I had some Linq expression there. Forgot to adjust the text. Edited now!
                      – r3dst0rm
                      Nov 19 at 11:44


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373469%2fgo-through-custom-type-list-and-return-the-first-object-where-a-certain-member-s%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Create new schema in PostgreSQL using DBeaver

                      Deepest pit of an array with Javascript: test on Codility

                      Costa Masnaga