P
Phil Ruelle
Hi,
Concise:
I've ben trying, unsuccessfully, to use the output from a LINQ query to
populate a generic List<T> object. Can someone please confirm that this isn't
actually possible using initialisation/"inline" code and actually requires a
loop (for example using the 'foreach' keyword or the ForEach extension
method)?
Verbose:
I'd like to use the object initializer syntax to populate a parent object
and all it's children. As an example consider the Parent class which has the
public property:
IList<IChild> Children
So ideally I'd like to write something along the lines of:
IParent mother = new Parent()
{ Name = "Mummy",
Children = new List<IChild>()
{ from c in data.children
select new Child()
{ Name =
c.firstname + c.familyname }
}
}
Can something like this be done? The problem seems to revovle around the
fact that the compiler looks for 'Add' methods on the collection class and
the only one available on IList and ICollection is one that takes IChild but
the LINQ query returns an IQueryable<IChild>. I've tried adding an extension
method to List<T> that accepts IQueryable<T> but the compiler does recognise
this when compiling the LINQ expression even though it does recorgnise it in
'normal' code.
I can get the correct output this by running the query outside of the
intialiser and then iterating through the results and calling add on the
Children collection but this situation just gets worse the more levels
(GrandChildren, GreatGrandChildren) are introduced.
Thanks in advance,
Phil
Concise:
I've ben trying, unsuccessfully, to use the output from a LINQ query to
populate a generic List<T> object. Can someone please confirm that this isn't
actually possible using initialisation/"inline" code and actually requires a
loop (for example using the 'foreach' keyword or the ForEach extension
method)?
Verbose:
I'd like to use the object initializer syntax to populate a parent object
and all it's children. As an example consider the Parent class which has the
public property:
IList<IChild> Children
So ideally I'd like to write something along the lines of:
IParent mother = new Parent()
{ Name = "Mummy",
Children = new List<IChild>()
{ from c in data.children
select new Child()
{ Name =
c.firstname + c.familyname }
}
}
Can something like this be done? The problem seems to revovle around the
fact that the compiler looks for 'Add' methods on the collection class and
the only one available on IList and ICollection is one that takes IChild but
the LINQ query returns an IQueryable<IChild>. I've tried adding an extension
method to List<T> that accepts IQueryable<T> but the compiler does recognise
this when compiling the LINQ expression even though it does recorgnise it in
'normal' code.
I can get the correct output this by running the query outside of the
intialiser and then iterating through the results and calling add on the
Children collection but this situation just gets worse the more levels
(GrandChildren, GreatGrandChildren) are introduced.
Thanks in advance,
Phil