-1

我正在尝试这样做....

var entity = landingPages.Select(x = new MyClass { name = x.name, age = x.age });

唯一的问题是年龄实际上是一种冷却,所以我想我会这样做......

var entity = landingPages.Select(x = new MyClass { name = x.name, age.add(x.age) });

但它不起作用。这可能吗?

4

1 回答 1

2
var entity = landingPages.Select(x => new MyClass
{  //this is an initialization block.  Only Property Assignment is allowed.
  name = x.name,
  age.add(x.age)  //arbitrary statements are not allowed in initialization blocks.
});

您没有说哪个年龄是集合(LandingPage 的年龄或 MyClass 的年龄)。

如果是 LandingPage 的年龄,则:将单个值转换为集合。

IEnumerable<MyClass> query = landingPages.Select(x => new MyClass
{
  name = x.name,
  age = new List<int>() { x.age } //this is a collection initializer
});

如果是 MyClass 的年龄,则:将集合转换为单个值。

IEnumerable<MyClass> query = landingPages.Select(x => new MyClass
{
  name = x.name,
  age = x.age.Sum()
});
于 2012-05-31T14:03:43.347 回答