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)))
{
}
}
}
c# list
add a comment |
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)))
{
}
}
}
c# list
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
add a comment |
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)))
{
}
}
}
c# list
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
c# list
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
add a comment |
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
add a comment |
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.
StartsWith
provides 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.
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
ThefilteredList
will hold aList<MP3objectSmall>
containing boths3
ands4
- but that was not what was asked for in the question... If you're going to uselinq
, I think you should just useFirstOrDefault
with the predicate you have. This will return only the first occurrence ordefault(MP3objectSmall)
(which isnull
) if not found.
– Zohar Peled
Nov 19 at 11:45
|
show 3 more comments
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
.
add a comment |
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);
}
}
add a comment |
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
FindIndex
is a method ofList
, 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
add a comment |
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.
StartsWith
provides 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.
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
ThefilteredList
will hold aList<MP3objectSmall>
containing boths3
ands4
- but that was not what was asked for in the question... If you're going to uselinq
, I think you should just useFirstOrDefault
with the predicate you have. This will return only the first occurrence ordefault(MP3objectSmall)
(which isnull
) if not found.
– Zohar Peled
Nov 19 at 11:45
|
show 3 more comments
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.
StartsWith
provides 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.
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
ThefilteredList
will hold aList<MP3objectSmall>
containing boths3
ands4
- but that was not what was asked for in the question... If you're going to uselinq
, I think you should just useFirstOrDefault
with the predicate you have. This will return only the first occurrence ordefault(MP3objectSmall)
(which isnull
) if not found.
– Zohar Peled
Nov 19 at 11:45
|
show 3 more comments
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.
StartsWith
provides 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.
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.
StartsWith
provides 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.
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
ThefilteredList
will hold aList<MP3objectSmall>
containing boths3
ands4
- but that was not what was asked for in the question... If you're going to uselinq
, I think you should just useFirstOrDefault
with the predicate you have. This will return only the first occurrence ordefault(MP3objectSmall)
(which isnull
) if not found.
– Zohar Peled
Nov 19 at 11:45
|
show 3 more comments
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
ThefilteredList
will hold aList<MP3objectSmall>
containing boths3
ands4
- but that was not what was asked for in the question... If you're going to uselinq
, I think you should just useFirstOrDefault
with the predicate you have. This will return only the first occurrence ordefault(MP3objectSmall)
(which isnull
) 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
|
show 3 more comments
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
.
add a comment |
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
.
add a comment |
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
.
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
.
edited Nov 19 at 11:32
answered Nov 19 at 11:27
Zohar Peled
51.6k73172
51.6k73172
add a comment |
add a comment |
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);
}
}
add a comment |
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);
}
}
add a comment |
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);
}
}
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);
}
}
answered Nov 19 at 11:42
Mat
1,25422029
1,25422029
add a comment |
add a comment |
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
FindIndex
is a method ofList
, 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
add a comment |
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
FindIndex
is a method ofList
, 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
add a comment |
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
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
edited Nov 19 at 11:43
answered Nov 19 at 11:26
r3dst0rm
602613
602613
FindIndex
is a method ofList
, 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
add a comment |
FindIndex
is a method ofList
, 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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