0

i have list of items IList with data that looks list this:

GenId     TestMode
  1          0
  1          1
  3          0
  3          1
  4        NULL
  2        NULL

i want to remove the index of GenId from my list that have TestMode == 0 if the same GenId has a TestMode == 1.

does someone have a terse way of doing this?

1 Answer 1

1

LINQ is very good at running operations against collections of objects. The following query should give you what you are looking for:

var query = list.Where(i => i.TestMode == 1 || 
                   !list.Exists(i2 => i2.GenId == i.GenId && i2.TestMode == 1));

foreach (var item in query) {
    // do something with items.
}

What this does is looks for an item where TestMode is equal to 1 (and includes if so), or otherwise checks to see if there is another element where TestMode is equal to 1, and excludes if that records exists.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.