How can I pass an anonymous LINQ result type to another method?

Possible duplicate:
How to pass an anonymous type to a method?

I want to pass a 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 represents a result similar to these "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 
+4
source share
3 answers

new { a, b, c, d, l } creates an element of an anonymous type , so the return value of a full LINQ query also results in an anonymous type. To pass such a value in a method, I would suggest converting it to a known type. Just enter a new class and interface if you want to abstract the method from a specific implementation:

It is unclear which request is executing and what types of elements are the class and type of update, respectively:

 interface IResultSet { TypeA A { get; } TypeB B { get; } TypeC C { get; } TypeD D { get; } TypeL L { get; } } class ResultSet : IResultSet { public ResultSet(inject all property values here) {} public TypeA A { get; private set; } public TypeB B { get; private set; } public TypeC C { get; private set; } public TypeD D { get; private set; } public TypeL L { get; private set; } } IEnumerable<IResultSet> 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 ResultSet(a, b, c, d, l); 

And keep in mind that LINQ Select() divided the execution, so the query itself will not be executed until you get access to the enumeration of the result set, so if you need to execute it immediately, just add the .ToList() call to end of request:

Deffered:

 ProcessData(sets); 

Immediate execution:

 ProcessData(sets.ToList()); // Now you can pass results in a method like this public void ProcessData(IEnumerable<IResultSet> items) 

And finally, I would suggest NOT using the dynamic type to abstract such anonymous types, you can use it, but in some special cases, so this will be an adequate solution. In your case, this will make the code less readable and break security type, dynamic great for DSL engines and things for processing dynamic structure data, but not to be a silver bullet for those who do not know the basics of OOP very well.

+4
source

You cannot pass an anonymous type directly to another method, since it is not:

First of all, you can pass it using the dynamic parameter:

 var at = new { a = 1, b = 2, c = 3, d = 4, l = 5 }; ProcessAnonymousType(at); void ProcessAnonymousType(dynamic at) { Console.WriteLine("Anonymous type data: {0}, {1}, {2}", at.a, at.b, at.c); } 

You can pass an instance of an anonymous type through an object and use reflection to access properties (in fact, this is very similar to the previous one, but with a lot of effort on your part):

 void ProcessAnonymousType(object at) { // Use reflection to access a, b, c properties } 

And, as always, you can use a named type.

+1
source

You can pass a typed parameter (as well as an anonymous type) as follows:

 void Method<T>(T obj) { } 

And then use reflection to extract all the public properties stored inside. Any variable can be passed as a parameter:

 var item = new { Foo = 1, Bar = "2" }; Method(item); 

And there will be no exceptions. As for reflection, this code

 object[] values = typeof(T) .GetProperties(BindingFlags.Instance | BindingFlags.Public) .Select(W => W.GetValue(obj, null)).ToArray(); 

Creates an array of public properties of your element inside the Method method. Result image .

0
source

All Articles