2014-05-09 33 views
0

我將如何返回一個唯一列表'listColl'我嘗試在'return listColl'的底部使用'Distinct9'''但收到一個錯誤,但它沒有奏效。目前這返回一個項目的重複列表,填充查詢中編碼的樹視圖。在asp-mvc4列表中返回不同的項目

 public List<SPList> GetAllLibraries(string webURL) 
    { 
     var listColl = new List<SPList>(); 
     ClientContext _ctx = new ClientContext(webURL); 
     try 
     { 
      var currentWeb = _ctx.Web; 
      var AllLists = currentWeb.Lists; 
      _ctx.Load(AllLists); 
      _ctx.ExecuteQuery(); 
      var query = from list in currentWeb.Lists 
         where list.BaseType == BaseType.DocumentLibrary 
         select list; 

      var listCollection = _ctx.LoadQuery(query.Include(myList => myList.Title, 
              myList => myList.Id, 
              myList => myList.RootFolder.ServerRelativeUrl, 
              myList => myList.ParentWebUrl, 
              myList => myList.Hidden, 
              myList => myList.IsApplicationList)); 
      _ctx.ExecuteQuery(); 

      // /* 
      listColl.AddRange(from list in listCollection 
           where !list.Hidden 
           select new SPList 
           { 
            Title = list.Title, 
            ListGUID = list.Id.ToString(), 
            RootFolderServerRelativeUrl = list.RootFolder.ServerRelativeUrl, 
            ParentWebUrl = list.ParentWebUrl 
           }); 
     // } */ 
      foreach (var Item in listCollection) 
      { 
       listColl.Add(new SPList 
       { 
        Title = Item.Title, 
        RootFolderServerRelativeUrl = Item.RootFolder.ServerRelativeUrl, 
        ListGUID = Item.Id.ToString() 
       }); 
      } 
     } 
     catch (Exception ex) 
     { 
      // error log 
      string error = ex.Message + " Error within GetAllLibraries "; 
     } 
     return listColl; 
    } 
+0

下面的答案沒有產生任何錯誤,他們也沒有解決這個問題。難道可能是Distinct的默認相等參數需要被覆蓋? – user3605501

回答

0

你能做到這一點嗎?

return listColl.Distinct(); 
0
listColl.AddRange((from list in listCollection 
          where !list.Hidden 
          select new SPList 
          { 
           Title = list.Title, 
           ListGUID = list.Id.ToString(), 
           RootFolderServerRelativeUrl = list.RootFolder.ServerRelativeUrl, 
           ParentWebUrl = list.ParentWebUrl 
          }).Distinct()); 
0

速戰速決的平均時間解決了這個問題。我無法做Distinct()的原因是因爲SPList。

List<SPList> distinct = new List<SPList>(); 
    List<String> ids = new List<string>(); 
    foreach (var lst in listColl) 
    { 
     if (!ids.Contains(lst.ListGUID)) 
     { 
      distinct.Add(lst); 
      ids.Add(lst.ListGUID); 
     } 
    } 
return distinct; 
相關問題