0

Must be a simple question, and I'm again close to a nervous breakdown because I can't find it:

I have a multimensional List that I defined as an own class (Id, Title, Desc, Start, Length, URL) and that I filled in one function

hyperlist.Add(
    new ListElement
    {
        Id = n,
        Title = title,
        Desc = desc,
        Start = OffsetTotal,
        Length = TagLength,
        URL = LinkURL
    });

I pass it to another function where I have to loop through it and compare each entry of the list to a parameter.

void BuildGList(List<ListElement> LinkList)
{
    int startIndex = 5;
    foreach (int Id in LinkList)
    {
        if(startIndex < Start)
        {
            ....
        }
    }
}

I don't see how to address each single column and googling it I get the impression that nobody uses lists to do what I want here.

-update-
I am asked to clarify my question. Lucky enough it had been clear to the ones that answered it: I didn't know how to refer to a special parameter in a List. Now I know that you can do it with item.parameter.
I'm really grateful for the help received in Stackoverflow but sometimes I get the impression that many of you experienced coders have little empathy and understanding for the problems a beginner faces and the effort it takes to google through a jungle of posts. Especially if you are a beginner and therefore sometimes miss the correct keywords. On this one I was busy for an hour and close to a breakdown as I knew I was catching really simple. If you know it then it's always easy. Cheers

Barnabeck
  • 459
  • 1
  • 6
  • 26
  • I don't get your meaning. What do you refer to by "address each single column"? Compare every property of your ListElement to properties of another instance? Or compare a specific property, such as Id, to some value? – galenus Jan 19 '16 at 12:53
  • 3
    These seems like a simple list of `T`, how is this multi-dimensional? The fact that your `T` is a class with properties does not make it multi-dimensional. – CodingGorilla Jan 19 '16 at 12:53
  • @galenus sorry for using SQL language, but that's what I am more used to, and I actually filled this List like a table. I'm learning c# and I didn't know about the "Item.parameter" syntax which is exactly the way to address this parameter of the list. Don't understand why you get downrated if you do not know something that is easy for many... but anyway. Thanks, my question is solved – Barnabeck Jan 19 '16 at 14:09
  • @Barnabeck I guess you were downvoted because the question was not phrased properly and people suspect that no preliminary research was performed. – galenus Jan 19 '16 at 14:12
  • Every experienced coder started out as an inexperienced coder, and I'd wager that all of us have at various points received comments like "I don't understand your question." When we receive such criticisms, the right answer is to do everything we can to clarify the question. Not only does it make it more likely that someone else will be able to help, but the act of asking a good question *in itself* makes us better coders. – Palpatim Jan 19 '16 at 15:33
  • @Palpatim off course you are right, I was more referring to the fact that one is easily suspected to post questions without previous research. I would have loved to be more clearer in that question. But unfortunately not having the knowledge doesn't allow me to be as precise as I wanted to; especially if one struggles with the correct terminology. I still don't see why my question was not understood and downrated several times... but I'm fine with the answers I got, that really helped me out. – Barnabeck Jan 19 '16 at 16:05
  • I share your frustration with stackoverflow and the tendency to badger those who are lacking basic knowledge that would allow them to ask a question in a format suitable to others. I swear that there are people floating around the site who must enjoy telling people their questions are no good, but are completely unwilling to provide an actual answer, because that would require a bit of thought and work whereas berating someone who simply needs help is rather easy. I for one, appreciated that your question was here, because although my problem was different, the answers here helped me out. – Alan Denke Sep 26 '22 at 16:49

3 Answers3

2

You can use foreach like this:

        foreach (ListElement item in LinkList)
        {
            if (item.Length < startIndex)
            {
                //Do something
            }
        }
1

You can filter the list using Linq e.g. to return an IEnumerable as the subset you could do:

private IEnumerable<ListElement> BuildGList(List<ListElement> linkList)
{
    int startIndex = 5;
    return linkList.Where(element => startIndex < element.Start);
}
ChrisProsser
  • 12,598
  • 6
  • 35
  • 44
1

You can use takewhile with a foreach if you want to use the list index:

foreach(var item in LinkList.TakeWhile((item, index) => index < startIndex))
{
    //enter your code here
}

Also, if you want to compare with a element value inside the list, you can use where with the foreach:

foreach(var item in LinkList.Where(item => item.Start < startIndex))
{
    //enter your code here
}
Striter Alfa
  • 1,577
  • 1
  • 14
  • 31