简体   繁体   中英

Compare two list based on condition C#

Below is the point that I want to implement

  1. Get TrackId by provided System Type (user input) and also keep note of the staging (user input (S,A,E,Q,P))
  2. Query SID list by TrackId && InUse = false
  3. Take result and access property Track eg 0A
  4. Check if there is any already existing SID which is InUse = true and has the same track "0A"
  5. If yes -> start over in step 2 and add to condition is not "0A"

Here are the two method which return the list

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

Now the question is I have list of Sid objects in tracklist , I want to check trackId property if its present in the second list with InUse property of second list is true, If its true I do not want to check it again and add condition that TrackId is check already and move on to next trackid from tracklist . Any idea?

Not completely sure what you want. But from what I understand you can try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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