English 中文(简体)
How to filter duplicate list items
原标题:
  • 时间:2009-11-09 16:39:20
  •  标签:
  • asp.net
  • linq

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?

最佳回答

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.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签