根据条件C#比较两个列表

时间:2020-10-14 11:16:10

标签: c# .net linq asp.net-core entity-framework-core

下面是我要实现的重点

  1. 通过提供的系统类型(用户输入)获取TrackId,并注意分段(用户输入(S,A,E,Q,P))
  2. 通过TrackId && InUse = false来查询SID列表
  3. 获取结果并访问属性Track(例如0A)
  4. 检查是否已存在InUse = true且具有相同轨道“ 0A”的SID
  5. 如果是->从第2步重新开始,并添加条件值不为“ 0A”

这是返回列表的两种方法

var tracklist = _sidRepo.GetTrackSidList(request.systemType);// return point no 2
        var SidList = _sidRepo.GetAllList();// return the whole sid list

现在的问题是我在tracklist中有Sid对象的列表,我想检查trackId属性是否存在于第二个列表中,而第二个列表的InUse属性为true,如果它为true,我不想再次检查它,然后添加条件TrackId已被检查,然后从tracklist移至下一个trackid。有想法吗?

1 个答案:

答案 0 :(得分:0)

不确定您想要什么。但是据我了解,您可以尝试一下

var tracklist = _sidRepo.GetTrackSidList(request.systemType);// return point no 2
var SidList = _sidRepo.GetAllList();// return the whole sid list
    
// Create a that will track if a trackId has already been checked
var trackIds = new List<int>();
var validTracks = new List<Sid>();

// we create a list of possible ids
var secondListIds = SidList.Select(x => x.TrackId);

// Loop through
foreach(var track in tracklist){

    // Check the exit condition
    if (trackIds.Contains(track.TrackId))
        continue;
        
    // Ok so we havn't seen this track ID before
    // We can check if it the Second list contains it
    if (secondListIds.Contains(track.TrackId))
        // It Does so we add it to the valid Track Ids
        validTracks.add(track);
        
    // Finally add the it to the check list     
    trackIds.Add(track.TrackId);
}