3

Possible Duplicate:
How can I pass an anonymous type to a method?

I have a terrible problem here, hope you can help me solve it

have the following linq-to-sql query, very simple:

var i = from cr in db.ComprobanteRecepcions  join c in db.Comprobantes
on new { cr.RFC, cr.RFCProveedor, cr.Folio, cr.Serie }  equals new { c.RFC, c.RFCProveedor, c.Folio, c.Serie }
where
Convert.ToString(cr.IDSucursal) == "4" &&
cr.RFC == "FIN020938SVR "
select new { cr.Serie, cr.Folio, cr.IDStatusComp, c.FechaEmision, c.Comentarios, c.Total };

I want to pass i to a method, something like this

mymethod void(var a)

Of course this can't be done... and creating a Type (class) to return it, something like this

select new MyType  {cr.Serie, cr.Folio, cr.IDStatusComp, c.FechaEmision, c.Comentarios, c.Total };

is impractical, such as returning a XElement or XDocument so what else can I do ?? I have to fill a datagrid with the var i variable and I don't know how to get this variable, I also googled for answers but there's not and easy one...

Understand that I'm a newbie using c#, .net and MS Technologies (I was a Java programmer)

Community
  • 1
  • 1
franko_camron
  • 1,288
  • 4
  • 14
  • 30
  • can't you use mymethod void(object a) ? – Nick B Jun 30 '11 at 23:10
  • Why's declaring the class impractical? It just needs to contain properties or member variables with all of the property names you set in your anonymous class. You can template your mymethod, although if you want to access the properties by name you either need to declare the class or maybe use 'dynamic' for the parameter. – Rup Jun 30 '11 at 23:13
  • Why is it impractical to create a class for this? – Ben Robinson Jun 30 '11 at 23:15
  • It's impractical because I have to make like 50 queries like this one. Using a class means that I'll have to creat 50 classes, one for each query, that's sounds like a lot of work to me... – franko_camron Jun 30 '11 at 23:32
  • 1
    @Nick Bradley, Ok let's suppose I can do that, how do I unbox the variable? – franko_camron Jun 30 '11 at 23:34
  • Why don't you just use the dynamic keyword like I suggested in the answer below?. It does exactly what you want – ingo Jun 30 '11 at 23:41
  • I solve it, I used the IEnumerable System.Collections.IEnumerable something like this: iEnum = from cr in ... then return it and use it to fill the datagrind, it worked – franko_camron Jul 01 '11 at 00:37
  • http://stackoverflow.com/questions/775387/passing-a-anonymous-type-to-function/4036301#4036301 – Roly Jul 01 '11 at 02:08
  • What does the method do that you want to pass the data to? What would it do with each of the 50 different types you would like to pass to it? The answer to that question might show you that you _do_ need to define 50 types, either implementing a particular interface, or sharing a common base type. – John Saunders Jul 01 '11 at 15:46

7 Answers7

4

What about returning something like this

public class MyType
{
    public ComprobanteRecepcions Recepcions { get;set; }
    public Comprobantes Comprobantes { get;set; }
}

and in your linq:

select new MyType { Recepcions  = cr, Comprobantes = c }
CRice
  • 12,279
  • 7
  • 57
  • 84
  • oh, well I'll have to create a class and that's something I don't want to, I have to make like 50 queries like this one. Using a class means that I'll have to creat 50 classes, one for each query. – franko_camron Jun 30 '11 at 23:36
  • You could make the properties in the class more general if there is commonality between your 50 classes. It hard to see the what you are trying to achieve. 50 unique queries is a lot. – CRice Jul 01 '11 at 00:32
2

Try look here:

LINQ: Can I pass a var as a parameter to a method?

or here:

Passing the parameter

var can only be used when a local variable is declared and initialized in the same statement; the variable cannot be initialized to null, or to a method group or an anonymous function.

Community
  • 1
  • 1
danyolgiax
  • 12,798
  • 10
  • 65
  • 116
1

The simplest way would be to make a concrete type to represent your results (your MyType above), instead of trying to pass an anonymous type around.

var is really only for local use, where the compiler can infer the types based on use. once you're passing that to another method, you have to use concrete types.

(although you could just make your method take "object" as its parameter, but you won't be able to do much with it after that except using reflection)

John Gardner
  • 24,225
  • 5
  • 58
  • 76
1

Use the dynamic keyword which came in .net 4

DoStuff(new { Message = "Hello Monkey"}); 

static void DoStuff(dynamic args)  
{                  
    Console.WriteLine(args.Message);  
}
ingo
  • 5,469
  • 1
  • 24
  • 19
1

So I solve it... Thanks to me, (great solution look:

System.Collections.IEnumerable i = from cr in db.ComprobanteRecepcions  join c in  db.Comprobantes
on new { cr.RFC, cr.RFCProveedor, cr.Folio, cr.Serie }  equals new { c.RFC,  c.RFCProveedor, c.Folio, c.Serie }
where
Convert.ToString(cr.IDSucursal) == "4" &&
cr.RFC == "FIN020938SVR "
select new { cr.Serie, cr.Folio, cr.IDStatusComp, c.FechaEmision, c.Comentarios, c.Total  };
return i;

And then in the datagrid I just took i as the datasource, bind it and voilà !!

franko_camron
  • 1,288
  • 4
  • 14
  • 30
  • Not quite the question you asked originally as you weren't specific about what you wanted to pass the results to in the question, but glad you found a solution to do what you wanted. – µBio Jul 01 '11 at 16:22
0

If the method you want to pass it to is your own, then depending on what you want to do (or how much reflection/convention you are willing to use, generics may be your answer

private string CheckMeOut<T>( T something )
{
    return something.GetType().Name;
}

public void CheckMeOutTest( )
{
    var anon = ( from x in typeof(string).GetMethods( )
                 select new {x.Name, Returns = x.ReturnType.Name} ).First( );
    string s = CheckMeOut( anon );
    Console.Out.WriteLine( s );
}
µBio
  • 10,668
  • 6
  • 38
  • 56
0

If you are using a SQL database, have you considered generating a data entity model (.edmx) from the database automatically? Then you can work with all the classes generated there and avoid this whole mess.

Nick B
  • 1,101
  • 9
  • 19