Original article: Dig a series of things that we don't commonly use in C # (1) -- todictionary, tolookup
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.
Class ticket {// <summary> // Ticket No. /// </Summary> Public String ticketno {Get; set ;} /// <summary> /// Order No. /// </Summary> Public int orderid {Get; set ;} /// <summary> /// remarks /// </Summary> Public String description {Get; Set ;}}
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.
Public static list <ticket> getlist () {return new list <ticket> () {new ticket () {ticketno = "999-12311", orderid = 79121281, description = "change"}, new ticket () {ticketno = "999-24572", orderid = 29321289, description = "refund"}, new ticket () {ticketno = "999-68904", orderid = 19321289, description = "deal"}, new ticket () {ticketno = "999-24172", orderid = 64321212, description = "not used"}, new ticket () {ticketno = "999-24579", orderid = 19321289, description = "refund"}, new ticket () {ticketno = "999-21522", orderid = 79121281, description = "not used"}, new ticket () {ticketno = "999-24902", orderid = 79121281, description = "refund"}, new ticket () {ticketno = "999-04571", orderid = 29321289, description = "change"}, new ticket () {ticketno = "999-23572", orderid = 96576289, description = "change"}, new ticket () {ticketno = "999-24971", orderid = 99321289, description = "deal "}};}
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<int, Ticket> dic = new Dictionary<int, Ticket>(); foreach (var item in ticketlist) { if (!dic.ContainsKey(item.OrderID)) { dic.Add(item.OrderID, item); } else { dic[item.OrderID] = 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.
VaR DIC = ticketlist. tolookup (I => I. orderid); foreach (VAR item in DIC) {console. writeline ("Order Number:" + item. key); foreach (VAR Item1 in item) {console. writeline ("\ t" + item1.ticketno + "" + item1.description );}}
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
Okay, this is probably the case. Stop the pen and don't talk about it... Sister urged her to go to Gu Cun Park. O (partition _ partition) O