2013-12-18 140 views
2

我使用一把umbraco的uQuery是需要兩個參數,並返回包含搜索結果列表的JSON序列串建設在C#中的Web服務Linq查詢。內建一把umbraco

我傳遞一個包含我的搜索標籤的字符串數組,例如[「紅色」,「藍色」]

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 

} 

到目前爲止很好,並且返回的結果包含我的任何標籤。

現在我想按日期限制結果。日期數組看起來像這樣[「201410」,「201411」],所以它是一年後的月份。

我想進一步限制我的結果設置爲那些有地方月份和年份匹配任何在我的約會對象數組的幾個月和幾年的指明MyDate財產的結果。

所以我的代碼變成這樣:

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 

} 

上面明顯能正常工作1個日期,但如果我通過在二按理說,一個頁面不能有兩個日期。

而且,我敢肯定有實現這一目標:)

感謝 特拉維斯

回答

1

目前查詢是確保一個更簡單的方法,該日期等於在日期的所有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可以包含所有的邏輯比較的日期,而不是試圖內聯所有的解析/比較。)

+0

優秀的感謝 – Travis