简体   繁体   English

.NET服务OData从LINQ查询返回匿名类型

[英].NET service OData return anonymous type from LINQ query

I have web api odata service that I need to return some data that was generated from a LINQ query that flattens an entity. 我有web api odata服务,我需要返回一些从LINQ查询生成的数据,这些数据会使实体变平。 For example I have a list of orders that I have used LINQ on the data service side to generate new properties such that I would like to return the number of orders that have a count between 0 and 100 then another property that would have a count between 101 and 200. etc. Here is a simplified example. 例如,我有一个订单列表,我在数据服务端使用LINQ生成新属性,以便我想返回计数介于0和100之间的订单数,然后是另一个具有介于两者之间的计数的属性101和200.等。这是一个简化的例子。

Public IHttpActionResult GetCustomData()
{
    var query = (from o in Orders
            select CustomerName = o.CustomerName,
            LowerCount = o.OrderDetails.Where(f => f.Pieces >= 0 && f.Pieces <= 100).Count(),
            MidCount = o.OrderDetails.Where(f => f.Pieces >= 101 && f.Pieces <= 200).Count(),
            HighCount = o.OrderDetails.Where(f => f.Pieces >= 201).Count()
            );

    return Ok(query);
}

In my WebApiConfig I the FunctionConfiguration with code like following: 在我的WebApiConfig中我使用以下代码的FunctionConfiguration:

FunctionConfiguration getCustomData = builder.EntityType<Order>().Collection.Function("GetCustomData");
getCustomData.Returns<Object>();

The url lets me call the function correctly but the json returned contains empty arrays. url让我正确调用函数,但返回的json包含空数组。 For example: if there are 6 records to return then the Json will have 6 empty [] inside. 例如:如果要返回6条记录,则Json内部将有6个空[]。

I also tried to return Newtonsoft.Json.Linq.JObject and used Newtonsoft to serialize the LINQ into Json but then I get a runtime error. 我还尝试返回Newtonsoft.Json.Linq.JObject并使用Newtonsoft将LINQ序列化为Json,但后来我遇到了运行时错误。
"The complex type 'JToken' has a reference to itself through the property 'Parent'. A recursive loop of complex types is not allowed." “复杂类型'JToken'通过属性'Parent'引用自身。不允许复杂类型的递归循环。”

I know I could create custom classes to fix this problem but there has to be some easier way to send generic Json to the client so that the client can unwrap and read the data. 我知道我可以创建自定义类来解决这个问题,但必须有一些更简单的方法将通用Json发送到客户端,以便客户端可以解包和读取数据。
I also know that I could send the entire order / order details to the client so that the client could "count" the data but I want to leave the payload small coming from the web service. 我也知道我可以将整个订单/订单详细信息发送给客户端,以便客户端可以“计算”数据,但我希望从Web服务中留下有效负载。
I feel that I am close since the client is getting back a json array but the array is empty. 我觉得我很接近,因为客户端正在返回一个json数组,但数组是空的。

You can use string as the return type. 您可以使用string作为返回类型。

Public IHttpActionResult GetCustomData()
{
    var query = (from o in Orders
            select CustomerName = o.CustomerName,
            LowerCount = o.OrderDetails.Where(f => f.Pieces >= 0 && f.Pieces <= 100).Count(),
            MidCount = o.OrderDetails.Where(f => f.Pieces >= 101 && f.Pieces <= 200).Count(),
            HighCount = o.OrderDetails.Where(f => f.Pieces >= 201).Count()
            );

    return Ok(JsonConvert.SerializeObject(query));
}

FunctionConfiguration: FunctionConfiguration:

FunctionConfiguration getCustomData = builder.EntityType<Order>().Collection.Function("GetCustomData");
getCustomData.Returns<string>();

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

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