简体   繁体   English

WCF OData服务是否支持投影?

[英]Does WCF OData Service Support Projection?

I'm using WCF OData service as my application Data Provider.OData service expose a entity that I don't want to get whole entity,I create LINQ query to get projection from this Entity. 我使用WCF OData服务作为应用程序数据提供程序。OData服务公开了一个我不想获取整个实体的实体,我创建了LINQ查询以从该实体获取投影。 But i have error in OData Service.This is my code: 但是我在OData Service中有错误。这是我的代码:

from n in NewsInfos
select new NewsInfos
{
    n.NewsId,
    n.NewsTitle,
    n.NewsLead,
    n.NewsDate
};

This is entire code: 这是完整的代码:

[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class NewsDataService : DataService<NewsODataModel>
    {
        public static void InitializeService(DataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
            config.DataServiceBehavior.AcceptProjectionRequests = true;
        }
    }

Yes, WCF Data Services and OData support projection. 是的,WCF数据服务和OData支持预测。 Projection is codified in the URL with the $select system query option, eg: http://services.odata.org/Experimental/OData/OData.svc/Products?$select=Name&$format=json . URL中的投影使用$select系统查询选项进行了编码,例如: http : //services.odata.org/Experimental/OData/OData.svc/Products ? $select =Name& $select format= json The LINQ Provider in the client bits enable this similarly to what you've shown in your example. 客户端位中的LINQ Provider启用了此功能,类似于示例中所示。 Here is one such example: 这是一个这样的例子:

using System;
using System.Data.Services.Client;
using System.Linq;

namespace Scratch
{
    public class Program
    {
        public static void Main()
        {
            var context = new DataServiceContext(new Uri("http://services.odata.org/OData/OData.svc/"));
            var categories = context.CreateQuery<Category>("Categories").Select(c => new { c.Name });
            Console.WriteLine("context.Categories.Where(...): {0}", categories);
            foreach (var category in categories)
            {
                Console.WriteLine(category.Name);
            }
        }
    }

    public class Category
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
}

One thing to consider with projection is that the magic in our client-side bits frequently requires you to use anonymous objects (hence the new { c.Name } ). 投影需要考虑的一件事是,客户端位中的魔力经常需要您使用匿名对象(因此使用了new { c.Name } )。

Your error may be unrelated; 您的错误可能无关。 if you're still getting the error after reading this can you update your service to return verbose errors as per http://blogs.msdn.com/b/phaniraj/archive/2008/06/18/debugging-ado-net-data-services.aspx ? 如果阅读此书后仍然出现错误,可以按照http://blogs.msdn.com/b/phaniraj/archive/2008/06/18/debugging-ado-net-更新服务以返回详细错误data-services.aspx My guess is that you may be missing the [DataServiceKey] attribute on NewsInfos . 我的猜测是您可能会缺少NewsInfos[DataServiceKey]属性。

Just return an anonymous object from your select and it should work. 只需从您的选择中返回一个匿名对象,它就可以工作。

from n in NewsInfos
select new
{
    n.NewsId,
    n.NewsTitle,
    n.NewsLead,
    n.NewsDate
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM