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)

9
  • can't you use mymethod void(object a) ? Commented Jun 30, 2011 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. Commented Jun 30, 2011 at 23:13
  • Why is it impractical to create a class for this? Commented Jun 30, 2011 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... Commented Jun 30, 2011 at 23:32
  • 1
    @Nick Bradley, Ok let's suppose I can do that, how do I unbox the variable? Commented Jun 30, 2011 at 23:34

7 Answers 7

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 }
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
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.

Comments

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)

Comments

1

Use the dynamic keyword which came in .net 4

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

static void DoStuff(dynamic args)  
{                  
    Console.WriteLine(args.Message);  
}

Comments

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à !!

1 Comment

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.
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 );
}

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.