简体   繁体   English

LINQ查询按顺序带回下一个项目

[英]LINQ Query to bring back next item in sequence

New to LINQ and stuck. LINQ的新手并卡住了。 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). 每个都有唯一的ID,为每个步骤分配给谁的ID以及一个序列(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. 我正在尝试传递步骤的唯一ID,但返回分配给下一步的人员的ID。 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. 因此,我试图传递ID 123,以返回ID 438。

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: 创建2个查询:

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: 另一个使用currentSequence进行下一步:

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

nextStep.Id gets you the ID of that step. nextStep.Id获取该步骤的ID。

Merged: 合并:

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

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);

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. 我创建了具有自己的类结构的示例,如OP所描述的那样,该类具有唯一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 为了解决这个问题,需要in My case Index of 3 is 1找到当前Id的索引, in My case Index of 3 is 1然后遍历列表,查看当前对象索引是否比用户提供的索引小1, 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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM