score:2
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();
score:0
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
.
score:0
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.
score:0
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();
Source: stackoverflow.com
Related Query
- Linq syntax to state explicit type instead of var
- Use anonymous type for LINQ based lists instead of var
- Declaring explicit type instead of var in select statement
- LINQ Join return explicit type and not VAR
- AsNoTracking using LINQ Query syntax instead of Method syntax
- Syntax to execute code block inside Linq query?
- Linq Query syntax vs. Method Chain: return type
- LINQ Source Code Available
- c# Convert LINQ var result to actual type
- linq join operator type with lambda syntax
- Cannot convert source type to target type List<KeyValuePair> Linq
- Type declaration in linq in lambda syntax
- creating Linq to sqlite dbml from DbLinq source code
- Linq to Sql NotSupportedException "The explicit construction of the entity in a query is invalid" when selecting new Type of other Database Table
- code first approach error: the specified type member 'yyyxx' is not supported in linq to entities
- LINQ ERROR : Explicit construction of entity type
- Replacing var with type name in Linq query
- How to Select top (5) contributors group by Business type code in c# linq query
- LINQ entity data model generated code error - The type 'DBContexts.Category' already contains a definition for 'ID'
- source code for LINQ 101 samples
- Syntax using LINQ ToList to cast GENERIC list to list of its base type
- LINQ var type cast to List
- Initializing var type in LINQ
- Linq Help please.. Direct casting to Object instead of Var
- LINQ Select syntax returns all columns instead of selected ones
- c# can I type the code I want to execute in LINQ expressions
- Cannot convert source type 'System Linq IQueryable<decimal>' to target type decimal
- Linq Projecting from one type to a second using fluent syntax
- MVC 5 - Why is my linq code selecting an entire query instead of a single value?
- c# Linq or code to extract groups from a single list of source data
More Query from same tag
- Compiled LINQ queries in .NET 4.5
- How to map a list of objects on another one with condition
- How to generate a full dynamic grid with Linq and JQuery DataTable?
- Some help on a Union LINQ expression please
- List: Detecting switches from less than zero to greater than zero
- LINQ with sub select query using sum and group by?
- linq to sql query help - common values
- Majority vote over strings that FAILS when equal
- What is the best way to query a database for records within n miles of a zip code?
- C# Creating a local string variable in linq - lambda expression
- combobox with multiple variables in one line, saving selection
- how to edit particular item in linq to xml
- Join multiple lists of objects in c#
- Select Distinct rows when only one columns is different Linq
- Set up self referencing class
- C# Select View from Oracle DB with LINQ
- MongoDB C# - .First() predicate not working in a projection
- Create duplicate items in a list
- Does modeling entity-relationship using SQL Server built-in "Diagrams" have performance benefits on the execution of queries?
- LINQ to Entities does not recognize the method 'System.String ToString()' method,and this method cannot be translated into a store expression
- How to optimize LINQ OrderBy if the keySelector is slow?
- Conditionally Adding Multiple Child XElements
- Filter in linQ c# with dynamic properties
- cannot convert from 'System.Collections.Generic.IEnumerable<int>' to 'System.Linq.IQueryable<int?>
- how return an IEnumerable<> of my customized model?
- Group By using Select statement in Linq
- Merge two linq expressions
- Generate a list of custom objects from Json object using LINQ
- Receiving Object reference not set to an instance of an object. on simple datetime retrieval from Raven
- Save Duplicate Values From String