1

So I am learning the basics of LINQ and I´m haveing a little trouble. How can I get the interval under entry name and exit name?? I'm having a hard time to solve this. this is the code

  List<list> list = new List<list>();
        station1 = new list()
        {
            no = 1,
            interval = 0,
            name = "name1",
            
        };
        station2 = new list()
        {
            no = 2,
            interval = 1,
            name = "name2",
        };
       station3 = new list()
        {
            no= 3,
            interval = 2,
            name = "name3",
        };
       station4 = new list()
        {
            no = 4,
            interval = 1,
            name = "name4",
        };
       station5 = new list()
        {
            no = 5,
            interval = 1,
            name = "name5",
        };

for example I enter the entry station and exit station (name1, name5) I want to add those interval inside the station under name1 and name5.

so the process will be output = name2.interval = 1 + name3.interval = 2 + name4.interval = 1 ;

total interval = 4

What I tried is, which is wrong and I am stuck:

    interval = list.GetRange(entry, exit);

This only gives me the interval of entry so I need to add a filter. Been trying this and that with no luck. If anyone could give me more hints or be of some assistance would be greatly appreciated

2
  • Where is the definition for list? Commented Sep 30, 2022 at 7:48
  • 1
    You use list both as a name for the variable containing the list, as well as for the class defining the structure of an item. Unless you deliberately want to obfuscate your code, this is a terrible idea. Do your co-workers and your future self a big favor and rename your class list { ... } to class Item { ... }. Commented Sep 30, 2022 at 7:49

2 Answers 2

4

I suggest using Skip and Take:

int total = list
  .OrderBy(item => item.no)                // if stations are not ordered  
  .SkipWhile(item => item.name != "name1") // skip before 1st station
  .Skip(1)                                 // skip 1st station
  .TakeWhile(item => item.name != "name5") // take up to the last station
  .Sum(item => item.interval);             // sum intervals
Sign up to request clarification or add additional context in comments.

1 Comment

While .OrderBy() illustrates the example functionality, it would be much wiser to sort the list once after creation (or after any change) and remove it here to avoid doing the sort on each sum call.
0

First, you'd have to get the no of stations with "name1" and "name5". After that, you can get the total with a LINQ query like this:

var no1 = list.First(x => x.name == "name1").no;
var no5 = list.First(x => x.name == "name5").no;
var total = list
  .Where(x => x.no > no1 && x.no < no5)
  .Sum(x => x.interval);

This sample assumes that the stations exist. After getting the no of the stations it filters the list for items with a no between the stations and afterwards builds the sum of the interval field for these items.

In addition, it iterates the list several times. If you want to find the stations by name more efficiently, you could change your list to a Dictionary<string, list> where name is the key and the item is the value. Then you can simply look the items up by name and iterate the list only once. In memory with a limited number of items, the difference will not be too big between the list and the dictionary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.