是否可以在 Linq 属性映射中设置默认排序依据?

发布于 2024-07-13 18:19:43 字数 54 浏览 7 评论 0原文

我可以在 Active Record 中执行此操作,但这对于 Linq to SQL 可行吗?

I it was possible to do this in Active Record, but is this feasible with Linq to SQL?

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

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

发布评论

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

评论(2

债姬 2024-07-20 18:19:43

您始终可以向分部类文件中的数据上下文/实体对象添加属性:

partial class MyDataContext {
    public IOrderedQueryable<Foo> FoosByName {
        get {return Foos.OrderBy(foo=>foo.Name);}
    }
}

然后从 FoosByName 启动的任何查询都将被预先排序(但仍然可组合)。

You can always add a property to the data-context / entities objects in a partial class file:

partial class MyDataContext {
    public IOrderedQueryable<Foo> FoosByName {
        get {return Foos.OrderBy(foo=>foo.Name);}
    }
}

Then any queries started from FoosByName will be pre-ordered (but still composable).

尘曦 2024-07-20 18:19:43

如果您使用存储过程或视图来检索数据,则可以。 然后,ORDER BY 可以位于存储过程或视图中,但 LINQ to SQL 不支持开箱即用。

You can if you use stored procedures or views to retrieve your data. The ORDER BY can then be in the stored proc or view but LINQ to SQL doesn't support this out of the box.

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