对象查询或IQueryable

发布于 2024-10-04 14:25:10 字数 462 浏览 1 评论 0原文

我使用 ObjectQuery 作为 winforms 应用程序中几个 BindingSource 的数据源。 问题是我习惯了 Linq 之类的查询,所以我使用它们定义数据源:

View.DsMyDataSource = (from p in ModelContext.GetContext.MyObject 
                       where p.Deleted == false  p) as ObjectQuery<MyObject>;

ModelContext.GetContext() 返回我的 modelContext 实体的单例。

这是这样做的好方法吗?我担心使用类似 LINQ 的查询可能会因为强制转换而丢失一些东西。

有没有其他方法可以使用 linq 语法获取 ObjectQuery 类型?

感谢您的任何提示。

I Use ObjectQuery as datasource for a couple of BindingSources in my winforms app.
Problem is that Im used to Linq like queries so I use them defining datasource:

View.DsMyDataSource = (from p in ModelContext.GetContext.MyObject 
                       where p.Deleted == false  p) as ObjectQuery<MyObject>;

ModelContext.GetContext() returns singleton of my modelContext entity.

Is it good way of doing that ? I'm afraid that using LINQ like queries I may loose something because of cast.

Is there any other way I can get ObjectQuery type using linq syntax ?

Thanks for any hint.

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

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

发布评论

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

评论(1

烧了回忆取暖 2024-10-11 14:25:10

事实上,ObjectQuery实现IQueryable,所以没有真正的区别。

我建议不要绑定到 IQueryable,因为这给 UI 带来了太多的权力。 IQueryable 的要点是将查询的执行推迟到稍后的时间点(例如 BLL、服务层),但在我看来已经太晚了。

当我说为时已晚时,我的意思是当查询到达 UI 时,不应再对数据库进行查询。但是,如果 UI 开始执行 .Count().Sum() 等操作,则会执行 2 个查询。使得处理数据上下文变得非常困难。

我的建议是,返回一个具体的集合:例如 ICollection,并绑定到它。

如果您需要执行分页之类的操作,请通过 LINQ .Skip().Take() 执行此操作。

Well actually ObjectQuery<T> implements IQueryable<T>, so there is no real difference.

I would advise to not bind to an IQueryable, as this is giving too much power to the UI. The point of IQueryable is to defer execution of queries to a later point in time (such as a BLL, services layer), but presentation IMO is too late.

When i say it is too late, i mean that by the time the query gets to the UI, no more queries against the database should be made. But if the UI starts performing operations such as .Count() or .Sum(), you have 2 queries executed. Makes it very difficult to dispose of your data context.

My advice, return a concrete collection: e.g ICollection<T>, and bind to that.

If you need to do things like paging, then do that via LINQ .Skip() and .Take().

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