ListItemCollection - get text from value with LINQ

listitemcollection get selected items
get all items from sharepoint list c#
get sharepoint list items programmatically c#
listitemcollection to list
listitemcollection sharepoint
caml query get all items
list item collection position
how to retrieve list items in c#

I have ListItemCollection. Each element is of type ListItem. I want to return only text with item that has value given.

For now, I am using this function:

public string GetTextFromPaymentWayCollection(string value)
{
  ListItemCollection listPaymentWays = DB.GetList();

  foreach (ListItem item in listPaymentWays)
  {
    if (item.Value == value)
    {
      return item.Text;
    }
  }
  return null;
}

Is there a way to do this with LINQ, instead of using this function?

Your listPaymentWays is of ListItemCollection that does not implement IEnumerable<T>. The linq methods are extension methods on IEnumerable<T>.

For example the signature of FirstOrDefault:

public static TSource FirstOrDefault<TSource>(
    this IEnumerable<TSource> source
)

What you should do is first Cast (which will return an IEnumerble<ListItem>) and then use the FirstOrDefault:

var result = listPaymentWays.Cast<ListItem>()
                            .FirstOrDefault(x => x.Value == value)?.Text;

The ?. is C# 6.0 Null Proparation feature.

Also I'd recommend that DB.GetList() will expose a method that will get the value so the filtering will happen in the database and not in memory. It is a waste to bring all the data, create ListItems of it to then just take a single item.

Lessons Learned: Make your ListItemCollection Linq Queryable, Lessons Learned: Make your ListItemCollection Linq Queryable. Sep 20, 2010 3 min read Suppose you'd like to get all of the checked items by the user. In such a case you would like to Add(new ListItem("text1", "value", false)); collection. I’m querying SharePoint site to get list item through client object model & able to get data. Then I used LINQ to get the array of “Name” values. ListItemCollection items = division.GetItems (query); context.Load (items); context.ExecuteQuery (); var test = (from c in items.

You need to Cast it first to use Where. The rest is just ordinary Linq.

string s = listPaymentWays.Cast<ListItem>()
                          .ToDictionary(i => i.Value, i => i.Text)
                          .Where(x => x.Value == value)
                          .FirstOrDefault().Value;

Querying Selected Items From ListItemCollection Using LINQ, Querying Selected Items From ListItemCollection Using LINQ. Working with ListItemCollection is general practices and getting single value for that it's like child's Here also we did cast to get the extension method Where. if we change text in lisitemcollection then it change the checkbox list items also. Dim qry = From i In ddlOutcome.Items.Cast(Of ListItem)() _ Where i.Text.Contains(value) qry.First().Selected = True The linq query seems preferable to iterating through the list myself, and I learned something in the process.

return listPaymentWays.FirstOrDefault(x => x.Value == value)?.Text;

Should work fine.

LINQ in Microsoft.SharePoint.Client.ListItemCollection, Then I used LINQ to get the array of “Name” values. ListItemCollection items = division. IEnumerable<string> items3 = from c in items2. select  Background on the Cast(): the .Cast<ListItem>() is required because the CheckBoxList's collection of Items is of type ListItemCollection, and it doesn't have the Where extension method. Here's the exception raised: Could not find an implementation of the query pattern for source type 'System.Web.UI.WebControls.ListItemCollection'. 'Where' not found.

Old question but you can use the already available (since .NET 1.1), non-LINQ method FindByValue:

return listPaymentWays.Items.FindByValue(value)?.Text;

How to: Retrieve List Items, ExecuteQuery(); foreach (ListItem oListItem in collListItem) { Console. To modify the previous example so that it returns the value of When you use LINQ to create queries against the client object model, you are using LINQ  Suppose you'd like to get all of the checked items by the user. In such a case you would like to query the CheckBoxList.Items collection. That's however not an IEnumerable<T> type but a ListItemCollection which just implements IEnumerable and won't therefore allow to be queried using Linq. So you would end up writing it the old style:

Converting ListItemCollection to List<t> efficiently, I want to filter it by using the Lookup Value in "Agents" with Linq but I don't understand how to be able to compare the lookup Value to a string. the code looks like  In the example, the ListItemCollection named listBoxData is used as the data source for a ListBox control called ListBox1, and the ListItemCollection called ddBoxData is used as the data source for a DropDownList control called DropDownList1.

Filter list with Linq and lookupvalue, GetItems() method to retrieve all the items (sorted and grouped as you've specified) that all the list items from a list where the Title column value starts with the word “Policy. Query = camlQueryText; SPListItemCollection items = list. is that LINQ provides strongly typed classes, whereas CAML is written as a string of text  Your listPaymentWaysis of ListItemCollectionthat does not implement IEnumerable<T>. The linq methods are extension methods on IEnumerable<T>. For example the signature of FirstOrDefault: public static TSource FirstOrDefault<TSource>( this IEnumerable<TSource> source) What you should do is first Cast(which will return an IEnumerble<ListItem>) and then use the FirstOrDefault:

SharePoint 2010 Six-in-One, I am applying these values to a ListItemCollection(LIC). Here is Loop the list items and check their uniqueness, ignoring the "Ignore" string Or, in a LINQ fashion, after you create the List<> object: select new { Item = g. Property Value ListItem. The ListItem object at the specified index in the collection. Examples. The following code example demonstrates how to select multiple items from a ListBox control and add the selected items to a DataGrid control.

Comments
  • Thanks, this works. What is this question mark at the end? Perhaps you have link to documentation about that?
  • Why convert to dictionary and then do Where? no need :)
  • Why I can't use FirstOrDefault on ListItemCollection?
  • @FrenkyB did you include System.Linq?