简体   繁体   English

格式化LINQ查询结果集

[英]Formatting LINQ Query Result Set

I am working on an ASP.NET MVC application. 我正在研究ASP.NET MVC应用程序。 This application executes a query via JQuery. 该应用程序通过JQuery执行查询。 The result set is returned as JSON from my ASP.NET MVC controller. 结果集从我的ASP.NET MVC控制器作为JSON返回。 Before I return the serialized result set, i need to trim it down to only the properties I need. 在返回序列化结果集之前,我需要将其缩减为仅需要的属性。 To do this, I'm using a LINQ Query. 为此,我正在使用LINQ查询。 That LINQ Query looks like the following: 该LINQ查询如下所示:

private IEnumerable RefineResults(ResultList<Result> results)
{           
  // results has three properties: Summary, QueryDuration, and List
  var refined = results.Select(x => new
  {
    x.ID, x.FirstName, x.LastName
  });

  return refined;
}

When I execute this method, I've noticed that refined does not include the Summary and Duration properties from my original query. 当我执行此方法时,我注意到,精炼不包括原始查询中的Summary和Duration属性。 I'm want my result set to be structured like: 我希望结果集的结构如下:

Summary
QueryDuration
Results
 - Result 1
 - Result 2
 - Result 3
 ...

At this time, when I execute RefineResults, I get the result list I would expect. 这时,当我执行RefineResults时,我会得到期望的结果列表。 However, I can't figure out how to put those entries in a property called "Results". 但是,我不知道如何将这些条目放在一个名为“结果”的属性中。 I also don't know how to add the "Summary" and "QueryDuration" properties. 我也不知道如何添加“摘要”和“ QueryDuration”属性。

Can someone please point me in the right direction? 有人可以指出正确的方向吗?

Thanks! 谢谢!

private object RefineResults(ResultList<Result> results)
{           
  // results has three properties: Summary, QueryDuration, and List
  var refined = results.Select(x => new
  {
    x.ID, x.FirstName, x.LastName
  });

  return new { Results = refined, Summary = results.Summary, QueryDuration = results.QueryDuration };
}

You will probably want to create a specific DTO class for the return value of this function. 您可能需要为此函数的返回值创建一个特定的DTO类。

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

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