Un esempio di come usare Linq to Object per manipolare i dati di una lista:
// Get all the Site Collections (Urls) in the "departments" Managed Path
var sites = from site in SPContext.Current.Site.WebApplication.Sites.Names
where site.ToLower().StartsWith("departments")
orderby site
select site;
// A List that will contain all the non-expired rolled up announcements
List
foreach (string siteName in sites)
{
using (SPSite spSite = new SPSite("http://intranet/" + siteName))
{
using (SPWeb web = spSite.RootWeb)
{
// Select the "top 2" announcements in each web
// where the announcement is not expired
var top2 =
(from all in
web.Lists["Announcements"].Items.GetDataTable().AsEnumerable()
orderby all.Field
where all.IsNull("Expires") == true ||
all.Field
select all).Take(2);
// Add those 2 announcements to multi Site Collection rollup
foreach (DataRow announcement in top2)
announcementsRollup.Add(announcement);
}
}
}
// Sort all the announcments across all the sites by created date
var sorted = from s in announcementsRollup
orderby s.Field
select s;
// Print all the announcements
foreach (DataRow announcement in sorted)
this.Controls.Add(new LiteralControl(announcement["Title"].ToString() + "
"));
End.
Nessun commento:
Posta un commento