开发者

How to filter duplicate list items

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 == 开发者_如何学Go1.

does someone have a terse way of doing this?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜