使用linq加入OData服务 [英] alternate of join in OData Service using linq

查看:120
本文介绍了使用linq加入OData服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在LINQ中加入两个表,我的查询是像

  SELECT mfg.MfgName 
FROM MasterProductStaging AS mps INNER JOIN
Mfg AS mfg ON mps.MfgID = mfg.MfgID
WHERE(mps.MasterProductStagingID = 345345)

现在在LINQ中,我使用两个不同的查询来解决这个问题

  var test =(从D在DataContext.MasterProductStagings 
其中d.MasterProductStagingID == Convert.ToInt32(Request.QueryString [MPSID])
选择d.MfgID).FirstOrDefault();

var data =(从D在DataContext.Mfgs
其中d.MfgID == test.ToString()
选择d).FirstOrDefault();

表在 emdx 模型。
现在我在单个查询中获取所需的数据。

解决方案

OData协议不支持JOIN,但是您仍然可以在单个请求中检索相关数据。以下是OData.org的几个例子:



http://services.odata.org/OData/OData.svc/Categories(1)/ $ links /产品



标识与类别1相关的产品集。
由相关服务元数据文档中类别实体类型上的名为产品的导航属性描述



http://services.odata.org/OData/OData.svc/Products(1)/ $ links / Category



标识与产品1相关的类别。
由相关服务元数据文档中产品实体类型上的名为类别的导航属性描述。



这应该给你一个OData链接的基本想法。如果您想使用LINQ与OData,您应该首先使用WCF数据服务创建一个代理类。那么你可以使用LINQ查询,但是要记住它们比实体框架更有限,例如他们不支持JOIN,但是支持一种替代方式来检索相关数据,例如:

  var ordersQuery = from o在context.Orders.Expand(Order_Details)
其中o.CustomerID ==ALFKI
选择O;


I want to join two tables in LINQ, my query is some thing like

SELECT        mfg.MfgName
FROM            MasterProductStaging AS mps INNER JOIN
                         Mfg AS mfg ON mps.MfgID = mfg.MfgID
WHERE        (mps.MasterProductStagingID = 345345)

Now in LINQ, I solve this problem using two different queries

    var test = (from d in DataContext.MasterProductStagings
                                        where d.MasterProductStagingID == Convert.ToInt32(Request.QueryString["MPSID"])
                                        select d.MfgID).FirstOrDefault();

var data = (from d in DataContext.Mfgs
                                        where d.MfgID == test.ToString()
                                        select d).FirstOrDefault();

The tables doesn't have any relation in emdx Model. Now I to get my desired data in a single query.

解决方案

OData protocol doesn't support JOIN, but you can still retrieve related data in a single request. Here are a couple of examples from OData.org:

http://services.odata.org/OData/OData.svc/Categories(1)/$links/Products

Identifies the set of Products related to Category 1. Is described by the Navigation Property named "Products" on the "Category" Entity Type in the associated service metadata document.

http://services.odata.org/OData/OData.svc/Products(1)/$links/Category

Identifies the Category related to Product 1. Is described by the Navigation Property named "Category" on the "Product" Entity Type in the associated service metadata document.

This should give you a basic idea of OData links. And if you want to use LINQ with OData, you should first create a proxy class using WCF Data Services. Then you can LINQ queries, but bear in mind that they are more limited than Entity Framework - for example they don't have support for JOIN, but support an alternative way of retrieving related data, e.g.:

var ordersQuery = from o in context.Orders.Expand("Order_Details")
    where o.CustomerID == "ALFKI" 
    select o;

这篇关于使用linq加入OData服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆