Does C# Distinct() method keep original ordering of sequence intact?

后端 未结 6 816
情话喂你
情话喂你 2020-11-29 11:01

I want to remove duplicates from list, without changing order of unique elements in the list.

Jon Skeet & others have suggested to use following

         


        
6条回答
  •  失恋的感觉
    2020-11-29 11:31

    This highly depends on your linq-provider. On Linq2Objects you can stay on the internal source-code for Distinct, which makes one assume the original order is preserved.

    However for other providers that resolve to some kind of SQL for example, that isn´t neccessarily the case, as an ORDER BY-statement usually comes after any aggregation (such as Distinct). So if your code is this:

    myArray.OrderBy(x => anothercol).GroupBy(x => y.mycol);
    

    this is translated to something similar to the following in SQL:

    SELECT * FROM mytable GROUP BY mycol ORDER BY anothercol;
    

    This obviously first groups your data and sorts it afterwards. Now you´re stuck on the DBMS own logic of how to execute that. On some DBMS this isn´t even allowed. Imagine the following data:

    mycol anothercol
    1     2
    1     1
    1     3
    2     1
    2     3
    

    when executing myArr.OrderBy(x => x.anothercol).GroupBy(x => x.mycol) we assume the following result:

    mycol anothercol
    1     1
    2     1
    

    But the DBMS may aggregate the anothercol-column so, that allways the value of the first row is used, resulting in the following data:

    mycol anothercol
    1    2
    2    1
    

    which after ordering will result in this:

    mycol anothercol
    2    1
    1    2
    

    This is similar to the following:

    SELECT mycol, First(anothercol) from mytable group by mycol order by anothercol;
    

    which is the completely reverse order than what you expected.

    You see the execution-plan may vary depending on what the underlying provider is. This is why there´s no guarantee about that in the docs.

提交回复
热议问题