1

I have a list like this

List<NamazTimesModel> res = nt.Select();

In res I have data like this

[
{
"name": "fajr",
"salahTime": "05:23",
"namazTime": "05:23",
"date": "3/6/2017 12:00:00 AM",
"endTime": null
},
{
"name": "sunrise",
"salahTime": "07:01",
"namazTime": "07:01",
"date": "3/6/2017 12:00:00 AM",
"endTime": null
},
{
"name": "zuhr",
"salahTime": "12:33",
"namazTime": "12:33",
"date": "3/6/2017 12:00:00 AM",
"endTime": null
},
....
]

I am looping over the list and checking what is the current item. For example it is 'fajr' then I have to take the next item and get the 'salahTime' from there and set 'endTime' of fajr to be that.

Can I get some help?

mohsinali1317
  • 4,255
  • 9
  • 46
  • 85

6 Answers6

1

You can skip list items while your target item is not found, then grab the target item itself, along with the next one:

var twoItems = res.SkipWhile(item => item.Name != "fajr").Take(2).ToList();

If you have exactly two items in twoItems list, then you found fajr, and it wasn't the last item on the list. If you have fewer than two items, then either fajr wasn't there, or it was the last item on the list.

Check that you have two items, and set fields as necessary:

if (twoItems.Count == 2) {
    twoItems[0].EndTime = twoItems[1].SalahTime;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Something like this should do it:

   var res = nt.Aggregate(new List<NamazTimesModel>(), (t, i) =>
            {
                if (t.Count > 0)
                {
                    t[t.Count - 1].endTime = i.salahTime;
                }
                t.Add(i);
                return t;
            }
        );
Baldrick
  • 11,712
  • 2
  • 31
  • 35
0
for(int i = 1; i < res.Count; i++)
{
   // Todo: Check for null references by your own way.
   res[i - 1].endTime = res[i].salahTime;
}
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
ravindra
  • 322
  • 3
  • 14
0

You can set Namaz times as below,

for(var i = 0; i<nts.Count()-1; i++){
 nt[i].EndTime = nt[i+1].SalahTime
}

Then don't forget to set last Namaz time:

nts[nt.Count()-1] = nts[0].SalahTime;
asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84
0
res.ForEach(item =>
{
     int index = res.FindIndex(m => m.name == item.name);
     item.endTime = index+1 != res.Count ? res.ElementAt(index + 1).salahTime : res.FirstOrDefault().salahTime;
});;
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
NikhilGoud
  • 574
  • 1
  • 5
  • 21
0

You can do two iterations with linq query.

It will be like this:

string[] numbers= {"one","two","last"}; string[] variables={"string one","int two","string three"}; var res = variables.Where(v => numbers.Any(n => v.Contains(n ))); res.ToList().ForEach(d => Console.WriteLine(d));

working code here.

Ygalbel
  • 5,214
  • 1
  • 24
  • 32