从查询中过滤数据的正确方法是什么?

时间:2014-06-13 07:37:02

标签: asp.net-mvc filtering

我创建了简单的查询,根据您的搜索参数返回结果。例如,如果您搜索内部号码:57774

您将得到以下结果:

Internal No.    Exchange Time Stamp  Order    Message Type    Exchange Time Stamp
57774           29.5.2014 8:50:00   3506709    DISP.LP    29.5.2014 8:50:00
57774           29.5.2014 8:50:00   3506709    DISP.LP    29.5.2014 8:50:00
57774           29.5.2014 8:50:00   3506709    DISP.LP    29.5.2014 8:50:00
057774          29.5.2014 8:49:08              DISP.NP    29.5.2014 8:49:08
057774          29.5.2014 8:49:08              DISP.NP    29.5.2014 8:49:08
057774          29.5.2014 8:49:08              DISP.NP    29.5.2014 8:49:08

现在我的新任务是,将所有这些结果放到一个显示此数据的简单行中,例如,对于内部编号57774,您将显示此简单行:

Internal No.   Exxhange Time Stamp    Order   Message Type   Exchange Time Stamp
057774         29.5.2014 8:49:08     3506709    DISP.LP      29.5.2014 8:50:00

所以这一行应该是这样的:

Internal No.: First internal number WHERE is First Date and Time AND If Message type stars with DISP.NP (if is not, return null)
Exch. Time Stamp: 29.5.2014 8:49:08  (First date)
Order: 3506709  (Last order number, if exisits)
Message Type: DISP.LP (Last message type)
Exch. Time Stamp: 29.5.2014 8:50:00 (Last message type)

有没有教程如何做到这一点,我想用Linq做这件事,我真的很感激任何想法。

我的查询代码是这样的(但它与问题无关,它只创建我的查询,它显示在我发布的第一个表中):

public ActionResult Index(SearchParameters parameters)
{
var order = new SortOrder[]
{new SortOrder("sequentialid", Order.DESC), new SortOrder("exchangetimestamp", Order.DESC)};
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<SpremenljivkeSolr>>();
var start = (parameters.PageIndex - 1) * parameters.PageSize;
var matchingSpremenljivke = solr.Query(BuildQuery(parameters), new QueryOptions
{
    Rows = parameters.PageSize,
    Start = start,
    OrderBy = order.Concat(GetSelectedSort(parameters)).ToList(),
});

var view = new SpremenljivkeView
{
    Spremenljivke = matchingSpremenljivke,
    Search = parameters,
    TimeZone = timeZone,

};

return View("../LoginUser/Index", view); 
}

public SortOrder[] GetSelectedSort(SearchParameters parameters)
{
    return new[] { SortOrder.Parse(parameters.Sort) }.Where(o => o != null).ToArray();
}

   public ISolrQuery BuildQuery(SearchParameters parameters)
   {
   if (parameters.Destination != null)

   {
    queryInList = new SolrMultipleCriteriaQuery(
            new[] { new SolrQueryInList("destination", parameters.Destination), new SolrQueryInList("sender", parameters.Destination) },
            "OR");
   }

  var solrQueries = new List<ISolrQuery> { queryInList };

  if (!string.IsNullOrEmpty(parameters.Order))
    solrQueries.Add(new SolrQueryByField("order", parameters.Order));


  if (parameters.Exchangetimestamp.HasValue && parameters.Exchangetimestamp2.HasValue)
  {
    solrQueries.Add(new SolrQueryByRange<DateTime>("exchangetimestamp",
        parameters.Exchangetimestamp.Value, parameters.Exchangetimestamp2.Value));
  }
  //and here is same thing solrQueries.Add for Internal

  return new SolrMultipleCriteriaQuery(solrQueries, "AND");

0 个答案:

没有答案