-2

I have a question, my friend and I are doing code, and he did two methods that I don't exactly understands.

public static List<Borrow> GetDistinctBorrows(List<Borrow> list)
{
    var Result = (from bor in list group bor by new { bor.BookAccessor, bor.ReaderAccessor } into dist select dist.First()).ToList();
    return Result;
}

and a second one

public static List<Borrow> GetDistinctBorrows(List<Borrow> list)
{
    var Result = list.GroupBy(x => new { x.ReaderAccessor, x.BookAccessor }).Select(y => y.First()).ToList();
    return Result;
}

Those methods have the same functionality, but one are written with LINQ, and a second one with lambda expressions.

Can someone explain to me, how they work (especially the fragment with 'new' word)?

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
KonradL
  • 9
  • 3
  • 2
    Just to clarify, both queries use LINQ: the first is *query syntax* and the second *method syntax*. Query syntax is mechanically transformed to method syntax on compilation. – Charles Mager May 24 '15 at 18:18
  • Read up here: https://msdn.microsoft.com/en-us/library/bb397947.aspx http://stackoverflow.com/questions/8037677/linq-query-syntax-vs-method-chains-lambda – Jeroen Vannevel May 24 '15 at 18:26
  • If I don't understand a friend of mine, I ask him/her, not StackOverflow. – Gert Arnold May 24 '15 at 21:04

2 Answers2

3

The part with new word is how you define instances on Anonymous Types.

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.

You create anonymous types by using the new operator together with an object initializer. For more information about object initializers, see Object and Collection Initializers (C# Programming Guide).

As a side note, your queries are equivalent, because compiler will transform the first one onto the second one as part of compilation process. Read more about that on MSDN: Query Syntax and Method Syntax in LINQ (C#)

Most queries in the introductory Language Integrated Query (LINQ) documentation are written by using the LINQ declarative query syntax. However, the query syntax must be translated into method calls for the .NET common language runtime (CLR) when the code is compiled. These method calls invoke the standard query operators, which have names such as Where, Select, GroupBy, Join, Max, and Average. You can call them directly by using method syntax instead of query syntax.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
0

The functions gets a list of distinct items from the input list, where ReaderAccessor and BookAccessor determine equality. Duplicated items from the input list is discarded.

They work by grouping by a new anonymous object defined by the two properties (this is where the new keyword is used), creates an alias dist and then taking the first one, essentially discarding the rest.

driis
  • 161,458
  • 45
  • 265
  • 341