我有一个带有 WHERE IN (ids) 的 IQueryable。 ToList() 可以返回与 ids 相同的顺序吗?

发布于 2025-01-11 06:49:07 字数 600 浏览 0 评论 0原文

我的代码:

var ids = new int[] { 16259,16238,16240,16243 };
var teamQuery = _teamRepository.Where(team => ids.Contains(team.ID));
var teams = teamQuery.ToList();

我有一个 IQueryable 对象 teamQuery ,它形成一个查询

SELECT
    [Extent1].[ID] AS [ID],
    [Extent1].[Name] AS [Name],
    FROM [dbo].[Team] AS [Extent1]
    WHERE [Extent1].[ID] IN (16259, 16238, 16240, 16243)

,我可以在 ToList() 实现期间在生成的 List teams 对象中保持与 IN 方法中相同的顺序( 16259、16238、16240、16243)?

现在,teams 中对象的顺序为 [16238, 16240, 16243, 16259]

My code:

var ids = new int[] { 16259,16238,16240,16243 };
var teamQuery = _teamRepository.Where(team => ids.Contains(team.ID));
var teams = teamQuery.ToList();

I have a IQueryable object teamQuery which forms a query

SELECT
    [Extent1].[ID] AS [ID],
    [Extent1].[Name] AS [Name],
    FROM [dbo].[Team] AS [Extent1]
    WHERE [Extent1].[ID] IN (16259, 16238, 16240, 16243)

Can I keep the same order in the generated List teams object during ToList() materialization as in the IN method (16259, 16238, 16240, 16243)?

Now the order of objects in teams is [16238, 16240, 16243, 16259]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来是傀儡 2025-01-18 06:49:07

这是您想要尝试和近似的 SQL:

SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Name] AS [Name],
FROM [dbo].[Team] AS [Extent1]
WHERE [Extent1].[ID] IN (16259, 16238, 16240, 16243)
ORDER BY CASE [Extent1].ID 
             WHEN 16259 THEN 0
             WHEN 16238 THEN 1
             WHEN 16240 THEN 2
             WHEN 16243 THEN 3
             ELSE 4
         END;

我不会推荐将其作为通用解决方案,除非您的选项列表被限制为非常小:

var ids = new [] { 16259, 16238, 16240, 16243 };
var firstId = ids.First();
var teamQuery = _teamRepository.Where(team => ids.Contains(team.ID))
                               .OrderByDescending(t => t.Id == firstId);
foreach(var id in ids.Skip(1))
{
    teamQuery = query.ThenByDescending(t => t.Id == id);
}
var teams = teamQuery.ToList();

更有效的解决方案是对结果进行排序 after< /em> 通过与原始数组索引相关联在数据库中执行:

var ids = new [] { 16259, 16238, 16240, 16243 };
var teamData = _teamRepository.Where(team => ids.Contains(team.ID)).ToList();
var teams = teamData.OrderBy(team => ids.IndexOf(team.ID)).Tolist();

要在纯 SQL 中有效地对大型列表执行此操作,需要首先创建有序列表作为表引用,然后您可以按该列表中的位置进行排序,但是LINQ to SQL 不会轻易复制相同的过程除非您在架构中定义特定的用户定义类型

您还可以插入这种类型的查询,或者将其构建为原始 SQL,这为您提供了更多选项,但不太像 LINQish。

一般来说,我们可以使用更高效的 C# 代码以这些自定义方式对数据进行排序。除非列表大得惊人,否则对数据库进行排序几乎没有什么好处。

This is the SQL that you want to try and approximate:

SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Name] AS [Name],
FROM [dbo].[Team] AS [Extent1]
WHERE [Extent1].[ID] IN (16259, 16238, 16240, 16243)
ORDER BY CASE [Extent1].ID 
             WHEN 16259 THEN 0
             WHEN 16238 THEN 1
             WHEN 16240 THEN 2
             WHEN 16243 THEN 3
             ELSE 4
         END;

I wouldn't recommend this as a generic solution, not unless your list of options was constrained to being very small:

var ids = new [] { 16259, 16238, 16240, 16243 };
var firstId = ids.First();
var teamQuery = _teamRepository.Where(team => ids.Contains(team.ID))
                               .OrderByDescending(t => t.Id == firstId);
foreach(var id in ids.Skip(1))
{
    teamQuery = query.ThenByDescending(t => t.Id == id);
}
var teams = teamQuery.ToList();

A more efficient solution is to sort the results after executing in the database by correlating against the original array indices:

var ids = new [] { 16259, 16238, 16240, 16243 };
var teamData = _teamRepository.Where(team => ids.Contains(team.ID)).ToList();
var teams = teamData.OrderBy(team => ids.IndexOf(team.ID)).Tolist();

To do it in pure SQL efficiently for a large list would involve first creating the ordered list as a table reference, then you could order by the position in that list, but the LINQ to SQL would not easily replicate the same process unless you define a specific User Defined Type in the schema.

You could also interpolate this type of query, or construct it as raw SQL, that provides you with more options but is less LINQish.

Generally we can sort data in these sorts of custom ways with more efficient code in C#. There are very few benefits to sorting on the database unless the list is obscenely large.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文