How to treat the distinctby extension and foreach extension of LINQ correctly

Source: Internet
Author: User
Tags foreach

In the Microsoft Standard LINQ, there is no distinctby extension and foreach extension, but in peacetime use of these two features are often used in the work, in the sense that Microsoft should include these two extensions in LINQ, but in fact why not? This article I would like to say that their understanding of these two extensions!

About Distinctby Extensions

As the name suggests, the Distinctby extension is based on a key value of the uniqueness of the filter, will have a duplicate key value of the elements removed, just keep one! Of course, there is a distinct extension in LINQ, but its function is simply a weak burst! Used comrades believe that the expansion of the distinct has countless times! If you want to use the distinct extension to implement the functionality of Distinctby extensions, you also need to specifically define a helper class! Oh,my god! This is a murder for the programmer! But after countless entanglements, I suddenly discovered that Microsoft actually contained the Distinctby Extensions in standard LINQ, but not so straightforward, look at the following sample code:

Class person
         {public
             int age {get; set;}
             public string Name {get; set;}
             public override string ToString ()
             {return
                 string. Format ("age:{0}  Name:{1}", age, Name);
             }
         [STAThread]
         static void Main ()
         {
             var persons = new[] { 
                 new person{age = 10,name= "A"},
                 new person{age = 10,name= "B" },
                 new person{age = 10,name= "C"},
                 new person{age = 20,name= "D"}
             };
             foreach (var p in persons.) GroupBy (O=>o.age). Select (G=>g.first ()))
             {
                 Console.WriteLine (p);
             }
         }

Run results

From the results, the code successfully completes the distinct operation based on the age of the key value.

The discerning eye may look at a glance, is good, is uses the GroupBy, select and first these three child operations to combine the Distinctby function! Just implement a distinctby function need to write this code is really bad, so you can encapsulate, the GroupBy, select and first of these three child operations packaged into a distinctby extension method, you can easily use. Encapsulated code too simple, it will not be posted!

About foreach Extensions

Why Microsoft does not add a foreach extension to LINQ, the discussion of this issue has been on the web a lot, summed up the main reasons for the following:

There is already a select extension in the 1.Linq, so it is not necessary to implement a foreach, because in most cases the Foreach function can be implemented with select (I do not recommend this, because select is a deferred operation, and if only the Foreach function is implemented, Some times the code is not able to operate correctly according to the design intent;

2.ForEach destroys the LINQ programming model, which means that the chain programming pattern is broken, and the so-called chained programming is probably meant to pass multiple operations through the dot number "." Linked together, I believe that the comrade wrote LINQ very clear the meaning of this sentence!

I've seen people on the web design a foreach extension like this:

public static void 

foreach<t> (this ienumerable<t> source, action<t> foreachaction)
         {
             foreach (Var t in source)
             {
                 foreachaction (t);
             }
         }

I have to say that this is simply a "sacrilege" of LINQ, Just imagine, if others use your this foreach, subsequent operation is not a myth? So the design of foreach is absolutely not! Even if you want to design, you have to design it!

public static ienumerable<t> foreach<t> (this 

ienumerable<t> source, action<t> Foreachaction)
         {
             foreach (var t in source)
             {
                 foreachaction (t);
             }
             return source;
         }

And it seems to me that it's not appropriate to add a foreach extension, because this design of a foreach extension destroys LINQ's latency-manipulation features!

So, I don't think Microsoft is the right choice to include a foreach extension in LINQ, and if you want to use the foreach feature, write it with a foreach statement! Actually it's not complicated!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.