top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How can we differentiate between Conversion Operator “ToDictionary” and “IEnumerable” of LINQ?

+3 votes
344 views
How can we differentiate between Conversion Operator “ToDictionary” and “IEnumerable” of LINQ?
posted Apr 20, 2015 by Merry

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes
 
Best answer

To solve the conversion type problems “IEnumerable” and “ToDictionary” conversion operator are used.

“ToDictionary” conversion operator is the instance of Dictionary (k, T). The “keySelector” predicate recognizes the key of each item, while “elementSelector”, is used to extract each single item, if it is given.

Extension method on “IEnumerable” is.AsEnumerable. AsEnumerable simply returns the source sequence as an object of type IEnumerable .

IEnumerable and To Dictionary both are Conversion Operator which are used to solved to conversion type Problems.

“AsEnumerable ” operator simply returns the source sequence as an object of type IEnumerable. This kind of “conversion on the fly” makes it possible to call the general-purpose extension methods over source, even if its type has specific implementations of them

Signature-

public static IEnumerable<T> AsEnumerable<T>
( 
this IEnumerable<T> source
);

“ToDictionary ” Conversion Operator is the instance of Dictionary (k,T) . The “keySelector ”predicate identifies the key of each item while “elementSelector ”, if provided, is used to extract each single item.

Key and elementSelector Predicate can be Used in following ways

Example

Public void ToDictionatyExample()
{ 
Var scoreRecords=
{
new {Name = "Alice",Score = 50 },
new {Name = "Bob",Score = 40 },
new {Name = "Cathy", Score = 45} };
Var scoreRecordsDict = scoreRecords.ToDictionary(sr => sr.Name);
Console.WriteLine("Bob's score: {0}", scoreRecordsDict("Bob"));
}

Result: Bob's score: { Name = Bob, Score = 40 }

answer Apr 20, 2015 by Shivaranjini
...