英文:
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<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
答案1
得分: 1
根据您的期望结果,您应该按以下方式排序:
-
Column1
的前两个字符以实现 QA > QB -
Column2
的第一个字符以实现 A > B
(可选)如果您希望按顺序进一步排序 Column1
和 Column2
:
-
Column1
-
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:
-
First 2 characters from
Column1
to achieve QA > QB -
First character from
Column2
to achieve A > B
(Optional) If you want to further ordering for Column1
and Column2
by sequence:
-
Column1
-
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();
答案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
中得到“重复”的结果,这将导致ThenBy
按Column2
的第一个字符进行排序。
另一种语法(完全相同):
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<FromTextFile> SortedList = ColumnsSwapped.OrderBy(o => o.Column1.Substring(0, 2)).ThenBy(o => 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<FromTextFile> SortedList = ColumnsSwapped.OrderBy(o => o.Column1[..2]).ThenBy(o => o.Column2[0]).ToList();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论