Passing variable of type var - unknown type

Go To


I have the following line of code:

var groups = inputRows.GroupBy(x => new { x.stateCIP, x.facultyRank });

I want to pass groups into another class method:


However, I do not know what type to declare the input parameter for the write method since it won't take var.

public static void write(?????? groups)

I have attached a photo of the message I get when I mouse over the var groups area:enter image description here

2012-04-03 21:21
by Baxter
How will you use "groups" inside the "write" method - daniloquio 2012-04-03 21:27
I will iterate over it in a foreach loop which has a sub-foreach loop to access the items one group at a time - Baxter 2012-04-03 21:29


One way would be to create a class with two properties, the stateCIP and facultyRank and then use that instead of creating an anonymous type that you are doing right now. Then you can pass that instead of the anonymous type into your method

so instead of var groups = inputRows.GroupBy(x => new { x.stateCIP, x.facultyRank })


var groups = inputRows.GroupBy(x => new MyClass(StateCIP = x.stateCIP, FacultyRank = x.facultyRank });

then you can do

public static void write(IEnumerable<MyClass> groups)

or if you want to do more with the groups objects that just iterate over them, like add or remove items from groups in the Write method, then in you calling method convert the IEnumerable to List using:

var groups = inputRows.GroupBy(x => new MyClass(StateCIP = x.stateCIP, FacultyRank = x.facultyRank }).ToList();

and then write your method as

public static void write(IList<T> groups)
2012-04-03 21:31
by Punit Vora
The compiler does not like either of those options - Baxter 2012-04-03 21:35
@baxter: i realized that the moment i submitted my answer. its updated now. this should work - Punit Vora 2012-04-03 21:41


Maybe something like this:

public class MyObject
     public string stateCIP { get; set; }
     public string facultyRank { get; set; }

The the call like this:

var groups = inputRows.GroupBy(x => new MyObject()
    stateCIP= x.stateCIP,
    facultyRank= x.facultyRank

And then the function like this:

public static void write(List<IGrouping<MyObject,YourTableEntity>> groups)
2012-04-03 21:36
by Arion


The solution to pass like a method is to pass like


for example you can write something like this:

public static void write(IEnumerable groups)

or you can make it templated like:

public static void write<T>(IEnumerable<T> groups)
2012-04-03 21:37
by Tigran
+1 for the inspiration for my answer. A better signature would be write<T>(IEnumerable<IGrouping<T, InputRow>>)phoog 2012-04-03 22:27


To give credit where it is due, this is an enhancement of Tigran's answer.

Your query returns an IEnumerable<IGrouping<'a, InputRow>>, so your method can generically accept that, if you replace the anonymous type with a type parameter (here T). By using a more specific type than just IEnumerable<T> (Tigran's suggestion), you get to treat the elements of the sequence as IGrouping objects:

public static void write<T>(IEnumerable<IGrouping<T, InputRow>> groups)
    foreach (var group in groups)
        foreach (var inputRow in group)
            //do something...

This will work very well if you do not need access to the members of the Key (i.e., the instances of the anonymous type). And here, you wouldn't need them, since those values are also available in the InputRow type, as implied by the query expression.

2012-04-03 22:24
by phoog


If you are using .NET 4.0 and you do not need the grouping information since it is in the individual items within the group you can use this:

public static void write(IEnumerable<IEnumerable<InputRow>> groups)
2012-04-03 23:13
by Daniel Baker