LINQ按两个列排序

huangapple go评论70阅读模式
英文:

LINQ to order by two columns

问题

这是我要翻译的部分:

"Hi I want to order by two columns using LINQ

This is the LINQ that I have:

List<FromTextFile> sortedList = ColumnsSwapped
    .OrderBy(o => o.Column1)
    .ThenBy(o => o.Column2)
    .ToList();

This is the result that I am getting:

    Column1; Column2
    QA7878729D; D9278787AQ
    QA7878736A; A6378787AQ
    QA7878751B; B1578787AQ
    QA7878834C; C4388787AQ
    QB7878726D; D6278787BQ

This is the result that I want:

    Column1; Column2
    QA7878736A; A6378787AQ
    QA7878751B; B1578787AQ
    QA7878834C; C4388787AQ
    QA7878729D; D9278787AQ
    QB7878726D; D6278787BQ

Is there something wrong with my LINQ? Am I not using .ThenBy() correctly?

I want to order Column1 QA > QB and for Column2 after ordering the Column1, it sorts from A > B.

    QA-A
    QA-B
    QA-C
    QA-D
    QB-D
```"

<details>
<summary>英文:</summary>

Hi I want to order by two columns using LINQ

This is the LINQ that I have:

```csharp
List&lt;FromTextFile&gt; sortedList = ColumnsSwapped
    .OrderBy(o =&gt; o.Column1)
    .ThenBy(o =&gt; o.Column2)
    .ToList();

This is the result that I am getting:

    Column1; Column2
    QA7878729D; D9278787AQ
    QA7878736A; A6378787AQ
    QA7878751B; B1578787AQ
    QA7878834C; C4388787AQ
    QB7878726D; D6278787BQ

This is the result that I want:

    Column1; Column2
    QA7878736A; A6378787AQ
    QA7878751B; B1578787AQ
    QA7878834C; C4388787AQ
    QA7878729D; D9278787AQ
    QB7878726D; D6278787BQ

Is there something wrong with my LINQ? Am I not using .ThenBy() correctly?

I want to order Column1 QA &gt; QB and for Column2 after ordering the Column1, it sorts from A &gt; B.

    QA-A
    QA-B
    QA-C
    QA-D
    QB-D

答案1

得分: 1

根据您的期望结果,您应该按以下方式排序:

  1. Column1 的前两个字符以实现 QA > QB

  2. Column2 的第一个字符以实现 A > B

(可选)如果您希望按顺序进一步排序 Column1Column2

  1. Column1

  2. Column2

List<FromTextFile> SortedList = ColumnsSwapped
    .OrderBy(o => o.Column1.Substring(0, 2))
    .ThenBy(o => o.Column2.Substring(0, 1))
    .ThenBy(o => o.Column1)
    .ThenBy(o => o.Column2)
    .ToList();
英文:

From your expected result, you should order by:

  1. First 2 characters from Column1 to achieve QA > QB

  2. First character from Column2 to achieve A > B

(Optional) If you want to further ordering for Column1 and Column2 by sequence:

  1. Column1

  2. Column2

List&lt;FromTextFile&gt; SortedList = ColumnsSwapped
    .OrderBy(o =&gt; o.Column1.Substring(0, 2))
    .ThenBy(o =&gt; o.Column2.Substring(0, 1))
    .ThenBy(o =&gt; o.Column1)
    .ThenBy(o =&gt; o.Column2)
    .ToList();

答案2

得分: 0

看起来你只想按Column1的前两个字符和Column2的第一个字符进行排序。如果是这样的话,你只需要将你的 Linq 改成:

List<FromTextFile> SortedList = ColumnsSwapped.OrderBy(o => o.Column1.Substring(0, 2)).ThenBy(o => o.Column2.Substring(0, 1)).ToList();

这样会忽略掉最后的字符,这意味着你会在Column1中得到“重复”的结果,这将导致ThenByColumn2的第一个字符进行排序。

另一种语法(完全相同):

List<FromTextFile> SortedList = ColumnsSwapped.OrderBy(o => o.Column1[..2]).ThenBy(o => o.Column2[0]).ToList();
英文:

It seems you only want to sort by the first 2 characters in Column1 and the first character in Column2. If that's the case, you only need to change your Linq to:

List&lt;FromTextFile&gt; SortedList = ColumnsSwapped.OrderBy(o =&gt; o.Column1.Substring(0, 2)).ThenBy(o =&gt; o.Column2.Substring(0, 1)).ToList();

That should ignore the last characters, which means you get 'duplicates' for Column1, which will cause the ThenBy to order by the first character in Column2.

Alternative syntax (does exactly the same):

List&lt;FromTextFile&gt; SortedList = ColumnsSwapped.OrderBy(o =&gt; o.Column1[..2]).ThenBy(o =&gt; o.Column2[0]).ToList();

huangapple
  • 本文由 发表于 2023年8月10日 10:49:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76872363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定