实体框架 - 如何在没有 LINQ 且仅使用字符串的情况下连接表?
我有一个关于实体框架的问题。如果您知道这方面的答案,请回答。我有这样的查询:
String queryRaw =
"SELECT " +
"p.ProductName AS ProductName " +
"FROM ProductEntities.Products AS p " +
"INNER JOIN CategoryEntities.Categories AS c " +
"ON p.CategoryID = c.CategoryID ";
ObjectQuery<DbDataRecord> query = new ObjectQuery<DbDataRecord>(queryRaw, entityContext);
GridView1.DataSource = query;
GridView1.DataBind();
特别是我想在一个查询中加入几个表,但我不能使用 LINQ,也不能将 ObjectQuery 与映射到查询中数据库字段的对象一起使用。因为每个实体都是动态创建的。所以这是我不能使用的:
msdn.microsoft.com/en-us/library/bb425822.aspx#linqtosql_topic12
msdn.microsoft.com/en-us/library/bb896339%28v=VS.90%29.aspx
< strong>问题是我可以使用这样的东西而不是使用对象吗?
query.Join ("INNER JOIN CategoryEntities.Category ON p.CategoryID = c.CategoryID ");
目的是使用 ObjectQuery 的 Join 方法,其语法如Where 方法:
msdn.microsoft.com/en -us/library/bb338811%28v=VS.90%29.aspx
谢谢,Artem
I have a question about Entity Framework. Please answer if you know answer on this. I have such query :
String queryRaw =
"SELECT " +
"p.ProductName AS ProductName " +
"FROM ProductEntities.Products AS p " +
"INNER JOIN CategoryEntities.Categories AS c " +
"ON p.CategoryID = c.CategoryID ";
ObjectQuery<DbDataRecord> query = new ObjectQuery<DbDataRecord>(queryRaw, entityContext);
GridView1.DataSource = query;
GridView1.DataBind();
Particularly I want to join few tables in one query, but I can NOT use LINQ and can NOT use ObjectQuery with objects mapped to DB fields inside my query. Because each entity creates dynamically. So this is what i can NOT use :
msdn.microsoft.com/en-us/library/bb425822.aspx#linqtosql_topic12
msdn.microsoft.com/en-us/library/bb896339%28v=VS.90%29.aspx
The question is can I use something like this instead of using objects?
query.Join ("INNER JOIN CategoryEntities.Category ON p.CategoryID = c.CategoryID ");
The purpose is to use Join method of ObjectQuery with syntax as in Where method :
msdn.microsoft.com/en-us/library/bb338811%28v=VS.90%29.aspx
Thanks, Artem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我现在看到的任何决定都是临时将 ObjectQuery 转换为字符串,将连接表添加为字符串,然后将其转换回 ObjectQuery :
也许有人有更一致的建议?
Any decision i see right now is to temporary convert ObjectQuery to string, add joined table as string and then convert it back to ObjectQuery :
Maybe someone has more consistent suggestions?
最后我找到了一个更好的解决方案,我们可以在主查询中使用子查询。例如:
这里有一些示例:
http://msdn.microsoft.com/en -us/library/bb896238.aspx
祝你好运!
Finally I Found a better solution for this, we can use Sub Query inside main Query. For example :
Some example is here :
http://msdn.microsoft.com/en-us/library/bb896238.aspx
Good luck!