TrevorGoodchild
TrevorGoodchild

Reputation: 1060

LINQ Query to bring back next item in sequence

New to LINQ and stuck. I have a collection of steps. Each has a unique ID, an ID for who the individual step is assigned to, and a sequence (0-7). I'm trying to pass in the unique ID of the step but return the ID of the person who is assigned to the next step. My current query looks like this (I know it's wrong, just trying to illustrate:

var nextApproverId = _context.ApprovalSteps
     .Where(p => p.Header.Active == true &&
     p.Sequence == (p.Sequence== (step.sequence + 1))
     .Select(p => p.AssignedApproverId);

The steps look like this

Id      AssignedApproverId      Sequence
123           100                  0
438           101                  1

So I'm trying to pass in Id 123 with the intention of returning Id 438.

Upvotes: 0

Views: 770

Answers (3)

Lucifer
Lucifer

Reputation: 1600

Here is possible answer, see output here

steps.Select(x =>steps.IndexOf(tags.Where(ele => ele.m_id == id).FirstOrDefault()) + 1).FirstOrDefault();

I created a sample with my own class structure as described by OP a person class with unique ID. To solve this OP needs to find the Index of current Id in My case Index of 3 is 1 then iterate over the list and see if current object index is 1 less then the one provided by user here In my case the index of output which is "2" is 2 so 2-1 == index of the Id provided as argument by OP

class person
{
  public string m_name;
  public int m_id;
  public person(string name,int id)
  {
     m_name = name;
     m_id = id;
  }
}

// Main class

List<person> steps= new List<person>();
steps.Add(new person("sample",1));
steps.Add(new person("sample1",3));
steps.Add(new person("sample2",2));

int id = 3;
int sample = steps.Where(x => steps.IndexOf(x) - 1 == (steps.IndexOf(steps.Where(ele => ele.m_id == id).FirstOrDefault()))).Select(x => x.m_id).FirstOrDefault();

int sample2 =  steps.Select(x =>steps.IndexOf(steps.Where(ele => ele.m_id == id).FirstOrDefault()) + 1).FirstOrDefault();

// output is 2 

Upvotes: 0

Hyarus
Hyarus

Reputation: 952

var currentId = 123;
var nextApproverId = _context.ApprovalSteps.Single(s => s.sequence == _context.ApprovalSteps.Single(p => p.id == currentId).sequence + 1).Id;
//Returns 438 in your example

Create 2 querys:

One to get the current sequence:

var currentSequence = _context.ApprovalSteps.Single(p => p.id == currentId).sequence;

The other to use the currentSequence to get the next step:

var nextStep = context.ApprovalSteps.Single(s => s.sequence == _currentSequence + 1);

nextStep.Id gets you the ID of that step.

Merged:

 var nextApproverId = _context.ApprovalSteps.Single(s => s.sequence == _context.ApprovalSteps.Single(p => p.id == currentId).sequence + 1).Id;

Upvotes: 2

Turbofant
Turbofant

Reputation: 531

Your Problem lies here:

 p.Sequence == (p.Sequence== (step.sequence + 1))

This part should be

 var nextApproverId = _context.ApprovalSteps
     .Where(p => p.Header.Active == true &&
     p.Sequence == (step.sequence + 1))
     .Select(p => p.AssignedApproverId);

Upvotes: 0

Related Questions