实体框架 - 如何在没有 LINQ 且仅使用字符串的情况下连接表?

发布于 2024-09-25 22:12:48 字数 992 浏览 5 评论 0原文

我有一个关于实体框架的问题。如果您知道这方面的答案,请回答。我有这样的查询:

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 技术交流群。

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

发布评论

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

评论(2

溺孤伤于心 2024-10-02 22:12:48

我现在看到的任何决定都是临时将 ObjectQuery 转换为字符串,将连接表添加为字符串,然后将其转换回 ObjectQuery :

RoutesEntities routesModel = new RoutesEntities(entityConnection);
String queryRaw = "SELECT " + 
                  "rs.RouteID AS RouteID, " +
                  "rs.LocaleID AS LocaleID, " + 
                  "rs.IsSystem AS IsSystem " +
                  "FROM RoutesEntities.Routes AS rs ";

_queryData = new ObjectQuery<DbDataRecord>(queryRaw, routesModel);

var queryJoin = _queryData.CommandText + " INNER JOIN LocalesEntities.Locales AS ls ON ls.LocaleID = rs.LocaleID ";
_queryData = new ObjectQuery<DbDataRecord>(queryJoin, routesModel);

也许有人有更一致的建议?

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 :

RoutesEntities routesModel = new RoutesEntities(entityConnection);
String queryRaw = "SELECT " + 
                  "rs.RouteID AS RouteID, " +
                  "rs.LocaleID AS LocaleID, " + 
                  "rs.IsSystem AS IsSystem " +
                  "FROM RoutesEntities.Routes AS rs ";

_queryData = new ObjectQuery<DbDataRecord>(queryRaw, routesModel);

var queryJoin = _queryData.CommandText + " INNER JOIN LocalesEntities.Locales AS ls ON ls.LocaleID = rs.LocaleID ";
_queryData = new ObjectQuery<DbDataRecord>(queryJoin, routesModel);

Maybe someone has more consistent suggestions?

牛↙奶布丁 2024-10-02 22:12:48

最后我找到了一个更好的解决方案,我们可以在主查询中使用子查询。例如:

var db = CustomEntity();

ObjectQuery<Categories> query1 = db.Categories.Where("it.CategoryName='Demo'").Select ("it.CategoryID");
var categorySQL = query1.ToTraceString().Replace("dbo", "CustomEntity"); // E-SQL need this syntax
ObjectQuery<Products> query2 = db.Categories.Where("it.CategoryID = (" + categorySQL + ")");

这里有一些示例:

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 :

var db = CustomEntity();

ObjectQuery<Categories> query1 = db.Categories.Where("it.CategoryName='Demo'").Select ("it.CategoryID");
var categorySQL = query1.ToTraceString().Replace("dbo", "CustomEntity"); // E-SQL need this syntax
ObjectQuery<Products> query2 = db.Categories.Where("it.CategoryID = (" + categorySQL + ")");

Some example is here :

http://msdn.microsoft.com/en-us/library/bb896238.aspx

Good luck!

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