简体   繁体   English

在umbraco中构建Linq查询

[英]Build Linq query in umbraco

I am building a web service in C# using Umbraco's uQuery that takes 2 parameters and returns a JSON serialized string containing a list of search results. 我正在使用Umbraco的uQuery在C#中构建Web服务,该服务使用2个参数并返回一个包含搜索结果列表的JSON序列化字符串。

I pass in a string array containing the tags for my search eg ["red", "blue"] 我传入一个包含用于搜索的标签的字符串数组,例如[“ red”,“ blue”]

public string GetResultsHttp(string[] tags)
{
    IEnumerable<Node> nodes;

    // first get all nodes that are of the right content type
    nodes = uQuery.GetNodesByType("MyPage");

    // if tags are passed in then limit the results to those tags
    if (tags.Length > 0)
    {
        nodes = nodes.Where(n => tags.Contains(n.Parent.GetProperty("tags").Value));
    }

    // return the node list as a serialized string

}

So far so good and that works to return results that contain any of my tags. 到目前为止,一切都很好,并且可以返回包含我的任何标签的结果。

now I want to limit the results by date. 现在我想按日期限制结果。 The dates array looks like this ["201410", "201411"] so it's year followed by month. date数组看起来像这样[[201410“,” 201411“],所以它是年份,后跟月份。

I want to further limit my result set to those results that have a myDate property where the month and year matches any of the months and years in my date array. 我想进一步将结果集限制为具有myDate属性的那些结果,其中月份和年份与我的日期数组中的任何月份和年份匹配。

So my code becomes this: 所以我的代码变成这样:

public string GetResultsHttp(string[] tags, string[] dates)
{
    IEnumerable<Node> nodes;

    // first get all nodes that are of the right content type
    nodes = uQuery.GetNodesByType("MyPage");

    // if tags are passed in then limit the results to those tags
    if (tags.Length > 0)
    {
        nodes = nodes.Where(n => tags.Contains(n.Parent.GetProperty("tags").Value));
    }

    if (dates.Length > 0)
    {
        // the format of the incoming date
        string formatString = "yyyyMM";

        foreach (string dateTag in dates)
        {
            DateTime dt = DateTime.ParseExact(dateTag, formatString, null);
            nodes = nodes.Where(n => (dt.Month.Equals(n.GetProperty<DateTime>("myDate").Month)) && (dt.Year.Equals(n.GetProperty<DateTime>("myDate").Year)));
        }
    }

    // return the node list as a serialized string

}

The above obviously works fine for 1 date, but if I pass in 2 it stands to reason that one page can't have 2 dates. 上面的代码显然可以在1个日期上正常工作,但是如果我输入2个,则可以认为一页不能有2个日期。

Also, I'm sure there is a much simpler way of achieving this :) 另外,我敢肯定有一种更简单的方法可以实现这一目标:)

Thanks Travis 谢谢特拉维斯

Currently your query is ensuring that the date is equal to All of the dates in dates . 当前,您的查询正在确保日期等于date中的所有 dates You want it to filter where Any of the dates are in dates . 您希望它过滤掉日期中的Any dates

var nodes= uQuery.GetNodesByType("MyPage")
    .Where(n => tags.Contains(n.Parent.GetProperty("tags").Value)
    .Where(n => dates.Any(dateString => 
        DatesAreEqual(dateString, n.GetProperty<DateTime>("myDate"));

( DatesAreEqual can contain all of the logic for comparing the dates, rather than trying to inline all of the parsing/comparing.) DatesAreEqual可以包含所有用于比较日期的逻辑,而不是尝试内联所有解析/比较。)

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

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