by Håvard Hebnes
20. June 2009 16:46
If you're looking for a specific item in a list, there are many ways you can get it.
You could go through the whole list using a loop (not recommended):
foreach (SPListItem item in list.Items)
{
if(item.Title == "Title1892")
return item;
}
You could use a SPQuery:
var tmpQuery = new SPQuery
{
Query =
@"<Where>
<Eq>
<FieldRef Name='Title' />
<Value Type='Text'>Title1892</Value>
</Eq>
</Where>"
};
list.GetItems(tmpQuery);
or you could use LINQ:
Method 1:
List<SPListItem> q = (from SPListItem item in list.Items
orderby item.Title
ascending
where item.Title == "Title1892"
select item).ToList();
Console.WriteLine(q[0].Title);
Method 2:
var query = from SPListItem item in list.Items
orderby item.Title
ascending
where item.Title == "Title1892"
select item;
Console.WriteLine(query.ElementAt(0).Title);