In this series, let's take a look at what we know in C #, but we don't know how to use it, or we are too lazy to understand, such as the toDictionary we will introduce.
And ToLookup.
We can see that there are four ToXXX methods, ToArray and ToList. I would like to use a lot of methods, but ToDictionary and ToLookup
Not necessarily many people use them, but it is undeniable that these methods are useful.
There is not much nonsense, such as the subject. We have such an entity, including the ticket number, order number, and remarks.
TicketNo { ; OrderID { ; Description { ;
Let's look at the requirement. The relationship between the ticket number and the order number is one-to-many. That is to say, an order number may contain several ticket numbers, and each ticket number has its own status,
For example, the ticket numbers include "change", "not used", "deal", and "refund". Below we fill in a batch of data.
List<Ticket> List<Ticket> Ticket(){ TicketNo=,OrderID=,Description= Ticket(){ TicketNo=,OrderID=,Description= Ticket(){ TicketNo=,OrderID=,Description= Ticket(){ TicketNo=,OrderID=,Description= Ticket(){ TicketNo=,OrderID=,Description= Ticket(){ TicketNo=,OrderID=,Description= Ticket(){ TicketNo=,OrderID=,Description= Ticket(){ TicketNo=,OrderID=,Description= Ticket(){ TicketNo=,OrderID=,Description= Ticket(){ TicketNo=,OrderID=,Description=
For example, I need to count the ticket numbers in each order number.
Obviously, this is a problem of grouping and sorting. You may immediately think of groupby. Of course, groupby can be implemented, but groupby cannot be a data type.
Structure. It cannot contain indexes and is easy to output and operate without dictionaries.
Solution 1: Use a general foreach loop.
In general, some people may use this original method to put the list data in the dictionary through the foreach loop, that is, the code is written more
Is the most flexible.
Dictionary<, Ticket> dic = Dictionary<, Ticket> ( item (!=
Solution 2: Use ToDictionary
We can see that, when a tragedy exception occurs, we know that the keys in the dictionary cannot be repeated, but the ToDictionary does not do it for us.
The duplicate value of the key indicates that ToDictionary can only be a "one-to-one" Relationship in kv, that is, there will always be only one record in v. Obviously, this is not
What I need is that this solution fails after I understand the ToDictionary principle.
Solution 3: Use ToLookup
Microsoft may know that the customer has such a requirement and adopts an enhanced ToDictionary version. You can also think of it as a new dictionary data structure, which avoids this
One-to-one relationship.
dic = ticketlist.ToLookup(i => ( item + ( item1 + item1.TicketNo + +
In addition, ToLookup is the same as a dictionary and has an index. This groupby is not available. Of course, Tolookup also has a powerful function that is used
Func <TSource, TElement> elementSelector is used to convert the current v element to avoid Console. WriteLine ("\ t" + item1.
TicketNo + "" + item1.Description); Statement