-2

这是一个有问题的基本类TheProperty

class BasicClass {
  public BasicClass() {
    TheProperty = new Object();
    Stamped = DateTime.Now;
  }
  public object TheProperty { get; set; }
  public DateTime Stamped { get; private set; }
}

这是基本列表:

class BasicList {
  private List<BasicClass> list;
  public BasicList() {
    list = new List<BasicClass>();
  }
  public BasicClass this[object obj] {
    get { return list.SingleOrDefault(o => o.TheProperty == obj); }
  }
  public void Add(BasicClass item) {
    if (!Contains(item.TheProperty)) {
      list.Add(item);
    }
  }
  public bool Contains(object obj) {
    return list.Any(o => o.TheProperty == obj); // Picked this little gem up yesterday!
  }
  public int Count { get { return list.Count; } }
}

我想添加一个类BasicList,它将返回一个项目数组。

我可以这样写,使用传统的 C#:

public object[] Properties() {
  var props = new List<Object>(list.Count);
  foreach (var item in list) {
    props.Add(item.TheProperty);
  }
  return props.ToArray();
}

...但是我将如何使用 LINQ 或 Lambda 查询来编写它?

4

1 回答 1

7
return list.Select(p=>p.TheProperty).ToArray()
于 2013-08-08T19:42:25.313 回答