1

All, I have the following class structure

public class Foo : IComparable<Foo> 
{
    public List<Bar> Bars;
}

public class Bar : IComparable<Bar> 
{
    public List<Con> Cons;
}

public class Con : IComparable<Con>
{
    ...
}

I know how to remove object from a list

authorsList.RemoveAll(x => x.FirstName == "Bob");

But how, for my class above, do I remove a List<Con> called badConList, from my base object Foo? Explicitly, the class hierarchy is populated like

Foo foo = new Foo();
foo.Bars = new List<Bar>() { /* Some Bar list */ };
foreach (Bar bar in foo.Bars)
    bar.Cons = new List<Con>() { /* Some Con list */ };

// Now a bad Con list.
List<Con> badCons = new List() { /* Some bad Con list */ };

How do I remove the badCons from foo for each Bar using LINQ?

Thanks for your time.

Ps. LINQ may not be the quickest method here; a simple loop might be (which is what LINQ will be doing under the hood anyway). Can you comment on this also?


Date , String , format java , android datepicker

I have a String which contains date in the format dd/mm/yyyy. For example lastDate = "1/2/2012" -> I get this date from date picker dialog. Now I have questions for this . I need to add 0 infront of date or month , if it is single digit . after the operation my date should be 20120201 ,any how I have done removing the delimeters and reversing the string , but I am stuck up , In adding 0 for day or month , if they are less than 10 , or single digit.

4

1 回答 1

3

您仍然可以使用RemoveAll

bar.Cons.RemoveAll(x => badCons.Contains(x));

另一种解决方案是使用循环:

foreach(var badCon in badCons)
    bar.Cons.Remove(badCon);

两个版本都会多次循环列表之一:

  1. 第一个版本循环badConsN 次,其中 N 为bar.Cons.Count()
  2. 第二个版本循环bar.ConsN 次,其中 N 为badCons.Count()

如果两个列表中的一个比另一个大,最好选择只循环大列表一次的版本,否则使用对您和代码库的读者更容易理解的版本。

于 2013-01-16T12:31:15.217 回答