Document directory
- Background
- ToLookup debut
- Source code download
- Conclusion
In this series, I learned some very useful functions from C # together with everyone. For some people, they may be inconspicuous, so they are often ignored. They won't yell: "Use me! I will save you a lot of time and make your code more concise !" -But these words will emerge from the minds of programmers who are familiar with them.
Okay! Let's take a look at ToLookup:
Translated from:
C #/. NET Little Wonders: Background of The ToLookup () LINQ Extension Method story
Let's first create a simple class to represent the product, the product has ID, category, and price. This class has nothing special:
public sealed class Product { public int Id { get; set; } public string Category { get; set; } public double Value { get; set; } public override string ToString() { return string.Format("[{0}: {1} - {2}]", Id, Category, Value); } }
Then we add a function to get a list of products. Of course, you can also read it from the database:
public static List<Product> GetList() { var products = new List<Product> { new Product {Id = 1, Category = "Electronics", Value = 15.0}, new Product {Id = 2, Category = "Groceries", Value = 40.0}, new Product {Id = 3, Category = "Garden", Value = 210.3}, new Product {Id = 4, Category = "Pets", Value = 2.1}, new Product {Id = 5, Category = "Electronics", Value = 19.95}, new Product {Id = 6, Category = "Pets", Value = 21.25}, new Product {Id = 7, Category = "Pets", Value = 5.50}, new Product {Id = 8, Category = "Garden", Value = 13.0}, new Product {Id = 9, Category = "Automotive", Value = 10.0}, new Product {Id = 10, Category = "Electronics", Value = 250.0}, }; return products; }
One task is to list an item list by category, which is very easy. You can use GroupBy:
foreach (var group in products.GroupBy(p => p.Category)) { Console.WriteLine(group.Key); foreach (var item in group) { Console.WriteLine("\t" + item); } }
Everything looks good, no problem.
When we useGroupBy ()Delayed execution is used for extension methods. This means that when you traverse the set, the next project that will appear may or may not be loaded. This is a huge performance improvement, but it can cause interesting side effects.
In useGroupBy (), It actually creates a group when the first item is used, insteadGroupBy ()When called for the first time.
Consider: If you load data from the database and want to combine the data and store it for quick search. See the following code:
Var groups = products. groupBy (p => p. category); // delete all products that belong to the Garden. removeAll (p => p. category = "Garden"); foreach (var group in groups) {Console. writeLine (group. key); foreach (var item in group) {Console. writeLine ("\ t" + item );}}
After the command is executed, it is found that all Garden products have disappeared, but groups has been assigned a value before executing the DELETE command.
Based on this situation, we have to use ToDictionary () to store the result after the GroupBy, so that the workload increases and the maintenance is not very convenient-please try.
ToLookup debut
Please ToLookup.
TheToLookup ()Method To createDictionary)But it is a new. NET Collection called lookup. Lookup, unlike Dictionary, cannot be changed. This means that once you create a lookup, you cannot add or delete elements.
var productsByCategory = products.ToLookup(p => p.Category);
foreach (var group in productsByCategory) { // the key of the lookup is in key property Console.WriteLine(group.Key); // the list of values is the group itself. foreach (var item in group) { Console.WriteLine("\t" + item); } }
You can also use the index-like function to get a project. In this case, you can get all products of a certain category:
private static void PrintCategory(ILookup<string, Product> productsByCategory,string categoryName) { foreach (var item in productsByCategory[categoryName]) { Console.WriteLine(item); } }
Source code download
You can download the source code of this case from here.
Conclusion
ToLookup ()Is a wonderful function used to operate a set and create a 1: n ing. It can easily classify data into groups and generate a dictionary for query.
Please note:
ASP. NET (Alex Song) of joy)