我如何返回一个唯一的列表'listColl'我尝试使用底部的'Distinct9'和'return listColl;'但收到一个错误。但它没有工作。目前这返回一个重复的项目列表... [英] How would I return a unique list 'listColl' I tried using 'Distinct9' at the bottom with 'return listColl;' but received an error.but it didnt work. Currently this return a duplicate list of items...
本文介绍了我如何返回一个唯一的列表'listColl'我尝试使用底部的'Distinct9'和'return listColl;'但收到一个错误。但它没有工作。目前这返回一个重复的项目列表...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public 列表< SPWeb> GetAllWebs( string webURL)
{
string url = System.Web.HttpUtility .UrlDecode(WEBURL);
列表< SPWeb> webColl = new 列表< SPWeb>();
尝试
{
ClientContext clientContext = new ClientContext(url) ;
Web oWebsite = clientContext.Web;
clientContext.Load(oWebsite,
website = > website.Webs,
website = > website.Title,
website = > website.Id,
website = > website.ServerRelativeUrl
);
clientContext.ExecuteQuery();
foreach (Web oneWeb in oWebsite.Webs)
{
webColl.Add( new SPWeb
{
Title = oneWeb.Title,
WebGUID = oneWeb.Id.ToString (),
ServerRelativeUrl =(url + oneWeb.ServerRelativeUrl).Replace( @ \, ),
});
}
}
catch (例外情况)
{
// 错误日志
string error = ex.Message + < span class =code-string> GetAllWebs中的错误;
}
返回 webColl;
}
public 列表< SPList> GetAllLibraries( string webURL)
{
var listColl = new List< SPList>();
ClientContext _ctx = new ClientContext(webURL);
尝试
{
var currentWeb = _ctx.Web;
var AllLists = currentWeb.Lists;
_ctx.Load(AllLists);
_ctx.ExecuteQuery();
var query = 来自 list currentWeb.Lists
where list.BaseType == BaseType.DocumentLibrary
select 列表;
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
其中!list.Hidden
select new SPList
{
Title = list.Title,
ListGUID = list.Id.ToString(),
RootFolderServerRelativeUrl = list.RootFolder.ServerRelativeUrl,
ParentWebUrl = list.ParentWebUrl
// 下面区分不工作
})。Distinct());
// } * /
foreach ( var 项 in listCollection)
{
listColl.Add( new SPList
{
Title = Item.Title,
RootFolderServerRelativeUrl = Item.RootFolder.ServerRelativeUrl,
ListGUID = Item.Id.ToString()
});
}
}
catch (例外情况)
{
// 错误日志
string error = ex.Message + < span class =code-string> GetAllLibraries中的错误;
}
// 返回错误
// return listColl.Distinct();
return listColl;
}
解决方案
嗨会员,
这样想吧 - DefaultDistinct
方法如何知道如何比较你的对象(类型SPList
)然后决定哪些是不同的? (它将使用默认的比较器,它不适合你 - 也许可以看到区别的msdn文档?)
只需使用重载列表。与IEqulaityComparere或为你的SPList类实现IEquatable。
http://msdn.microsoft.com/EN-US/library/bb348436(v = vs.110)的.aspx
public List<SPWeb> GetAllWebs(string webURL)
{
string url = System.Web.HttpUtility.UrlDecode(webURL);
List<SPWeb> webColl = new List<SPWeb>();
try
{
ClientContext clientContext = new ClientContext(url);
Web oWebsite = clientContext.Web;
clientContext.Load(oWebsite,
website => website.Webs,
website => website.Title,
website => website.Id,
website => website.ServerRelativeUrl
);
clientContext.ExecuteQuery();
foreach (Web oneWeb in oWebsite.Webs)
{
webColl.Add(new SPWeb
{
Title = oneWeb.Title,
WebGUID = oneWeb.Id.ToString(),
ServerRelativeUrl = (url + oneWeb.ServerRelativeUrl).Replace(@"\", ""),
});
}
}
catch (Exception ex)
{
// error log
string error = ex.Message + " Error within GetAllWebs ";
}
return webColl;
}
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
//below Distinct not working
}).Distinct() );
// } */
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 error
//return listColl.Distinct();
return listColl;
}
解决方案
Hi Member,
Think it this way - how should the DefaultDistinct
method "know" how to compare your objects (of typeSPList
) and then decide which are distinct? (it will use the Default comparer which will not work for you - maybe see the msdn documentation for Distinct?)
Just use the overload list.Distinct with an IEqulaityComparere or implement IEquatable for your SPList class.
http://msdn.microsoft.com/EN-US/library/bb348436(v=vs.110).aspx
这篇关于我如何返回一个唯一的列表'listColl'我尝试使用底部的'Distinct9'和'return listColl;'但收到一个错误。但它没有工作。目前这返回一个重复的项目列表...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文