user3904868
user3904868

Reputation:

Linq syntax to state explicit type instead of var

I'm trying to write a LINQ query to get List<List<testobject>> results = ...

I get the correct results using var but want to declare the explicit type rather than use var. What is the correct syntax to do this?

Simple example is as follows

class Program
{
    static void Main(string[] args)
    {

        List<testobject> testobjectList = new List<testobject>()
        {
            new testobject(){field1 = 1, field2 = "1",field3 = "1",field4 = "1", field5 = "1"},
            new testobject(){field1 = 1, field2 = "1",field3 = "1a",field4 = "1a", field5 = "1a"},
            new testobject(){field1 = 1, field2 = "1",field3 = "1b",field4 = "1b", field5 = "1b"},
            new testobject(){field1 = 2, field2 = "2",field3 = "2",field4 = "2", field5 = "2"},
            new testobject(){field1 = 3, field2 = "3",field3 = "3",field4 = "3", field5 = "3"},
            new testobject(){field1 = 4, field2 = "4",field3 = "4",field4 = "4", field5 = "4"},
            new testobject(){field1 = 4, field2 = "4",field3 = "4a",field4 = "4a", field5 = "4a"},
            new testobject(){field1 = 5, field2 = "5",field3 = "5",field4 = "5", field5 = "5"},
            new testobject(){field1 = 6, field2 = "6",field3 = "6",field4 = "6", field5 = "6"},
            new testobject(){field1 = 6, field2 = "6",field3 = "6a",field4 = "6a", field5 = "6a"},
            new testobject(){field1 = 6, field2 = "6",field3 = "6b",field4 = "6b", field5 = "6b"},
            new testobject(){field1 = 7, field2 = "7",field3 = "7",field4 = "7", field5 = "7"}

        };

        // Correct output
        var results1 = testobjectList.Where(x => x.field1 >= 2)
                                     .GroupBy(x => x.field2).ToList();

        // But how do I do the same but explicitly state type?
        List<List<testobject>> results2 = testobjectList.Where(x => x.field1 >= 2)
                                                        .GroupBy(x => x.field2).ToList();
    }
}

class testobject
{
    public int field1 { get; set; }
    public string field2 { get; set; }
    public string field3 { get; set; }
    public string field4 { get; set; }
    public string field5 { get; set; }
}

Upvotes: 2

Views: 478

Answers (4)

Tinypond
Tinypond

Reputation: 51

You need to flatten your result by adding a .SelectMany() to strongly type the result as a List.

You also have the List declared as List<List> result 2 when it should be List

Working Sample:

List<testobject> results2 = testobjectList .Where(x => x.field1 >= 2)
                                                    .GroupBy(x=>x.field2)
                                                    .SelectMany(x=>x)
                                                    .ToList();

Upvotes: 0

Saman Gholami
Saman Gholami

Reputation: 3512

First of all when you are using GroupBy function, var is compiled to:

List<IGrouping<string,testobject>>

If you really want to have List<List<testobject>> you can use this query:

testobjectList.Where(x => x.field1 >= 2).GroupBy(x => x.field2).Select(x => x.ToList()).ToList();

And if you want to have List<testobject> you can use:

testobjectList.Where(x => x.field1 >= 2).GroupBy(x => x.field2).Select(x => x.First()).ToList();

Upvotes: 2

Jonas Benz
Jonas Benz

Reputation: 503

When you hover over the ToList() call in VisualStudio you can see that the return type is List<IGrouping<string, testobject>>

Try:

List<IGrouping<string, testobject>> results2 = testobjectList.Where(x => x.field1 >= 2)
                                                             .GroupBy(x => x.field2).ToList();

If you have ReSharper you can use the "Specify type explicitly" refactoring to convert the var statement to the explicit type.

Upvotes: 0

Justinas Marozas
Justinas Marozas

Reputation: 2692

List<List<testobject>> wouldn't be the correct type. If you added a breakpoint on a line after and results1 is declared and inspected the type of it you would probably see something similar to List<IGrouping<string, testobject>>. If you want to declare explicit types use some means to figure out actual type, like a debugger, IDE or some plugin. ReSharper could give you a refactoring option to declare explicit type instead of var.

Upvotes: 0

Related Questions