This is my class, for example :
public class Point
{
public string Min { get; set; }
public string Max { get; set; }
public Point()
{
}
}
and I’m building dynamic objects through linq to xml :
var list = xDoc.Descendants("item").Select(item => new
{
NewPoint = new Point()
});
Now, I’d like to associate for each NewPoint the value item.Min and item.Max.
Such as NewPoint.Min = item.Min and NewPoint.Max = item.Max, without creating a Class constructor with 2 param in the method.
Is it possible? Hope the question is clear…
You can use an object initializer:
(or however you get your values out of
n)Alternatively, you can put a whole code block in your
Select:Also note: unless you’re selecting other things as well, you don’t need
You can just use
n => new Point()and end up with anIEnumerable<Point>rather than anIEnumerable<AnonymousClassContainingPoint>.