Errata corrige for sample code 5.6 of my book
I’ve just found (thanks to a brilliant reader!) that code sample 5.6 of my book is wrong. Here you can see the right version of the code excerpt:
// Listing 5-6
static void BrowseForContactsWithLINQ()
{
// Open the current ClientContext
ClientContext ctx = new ClientContext(“http://devbook.sp2010.local/“);
// Prepare a reference to the current Site Collection
Site site = ctx.Site;
ctx.Load(site);
// Prepare a reference to the current Web Site
Web web = site.RootWeb;
ctx.Load(web);
// Prepare a reference to the list of “DevLeap Contacts”
List list = web.Lists.GetByTitle(“DevLeap Contacts”);
ctx.Load(list);
// Execute the prepared commands against the target ClientContext
ctx.ExecuteQuery();
// Show the title of the list just retrieved
Console.WriteLine(list.Title);
// Prepare a query for all items in the list
CamlQuery query = new CamlQuery();
query.ViewXml = “
ListItemCollection allContacts = list.GetItems(query);
var linqQuery =
from c in allContacts
where (String)c[“DevLeapCountry”] == “Italy”
select c;
var linqQueryResult = ctx.LoadQuery(linqQuery);
// Execute the prepared command against the target ClientContext
ctx.ExecuteQuery();
// Browse the result
Console.WriteLine(“\nContacts”);
foreach (ListItem listItem in linqQueryResult)
{
Console.WriteLine(“Id: {0} – Fullname: {1} – Country: {2}”,
listItem[“DevLeapContactID”],
listItem[“Title”],
listItem[“DevLeapCountry”]
);
}
}
I’ve highlighted in bold the fixed code.