我如何返回一个唯一的列表'listColl'我尝试使用底部的'Distinct9'和'return listColl;'但收到一个错误。但它没有工作。目前,这将返回一个重复的项目列表,其中填充了在查询中编码的树视图。
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 :(得分:0)
你能这样做吗?
return listColl.Distinct();
答案 1 :(得分: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());
答案 2 :(得分: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;