Possible Duplicate:
How can I pass an anonymous type to a method?
I want to pass the collection of LINQ result to another method
This is the LinQ code
var sets =
from a in patient
from b in patient
from c in patient
from d in patient
from l in patient
where a.VisitNum < b.VisitNum && b.VisitNum < c.VisitNum && c.VisitNum < d.VisitNum && d.VisitNum < l.VisitNum
select new { a, b, c, d, l };
The query present the result like this “combinations”
ID Visit DAte Visit number Rational
-------------------------------------------------
a- 1 14/05/2011 1 new
b- 1 15/06/2012 2 Emergency
c- 1 17/07/2012 3 Check-Up
a- 1 14/05/2011 1 new
b- 1 15/06/2012 2 Emergency
c- 1 18/12/2012 5 Check-Up
new { a, b, c, d, l }creates an item of anonymous type so return value of entire LINQ query results in an anonymous type as well. To pass such value in a method I would suggest converting it to a known type. Just introduce a new class and an interface if you would like abstract a method from a concrete implementation:It is not clear what query does and which item types are so update class and type names accordingly:
And keep in mind that LINQ
Select()has deffered execution so query itself will not be executed until you access result set enumeration, so if you need to execute it immediately just add.ToList()call at the query end:Deffered:
Immediate execution:
And finally I would suggest NOT using
dynamictype to abstract such anonymous types, you can use it but in some special cases so it would be adequate decision. In your case it will makes code less readable and broke type safety,dynamicperfectly fits for DSL engines and things to handle dynamic structure data but not to be a silver bullet for those who badly know OOP basics.