DangerCoder
DangerCoder

Reputation: 71

Using Linq(?) to get a property from a list inside of a list

I need help to select all Titles from List"FeedItem" that is inside of List"Feed" where Feed.Name matches a string from a combobox.

Below is my attempt, which is not succesful, might be on the wrong road.

 var loadFeedData = fillFeed.GetAllFeeds();
            var filteredOrders =
            loadFeedData.SelectMany(x => x.Items)
                 .Select(y => y.Title)
                 .Where(z => z.Contains(flow)).ToList();

To understand things better I'll post the Feed.cs code as well.

public class Feed : IEntity
{
    public string Url { get; set; }
    public Guid Id { get; set; }
    public string Category { get; set; }
    public string Namn { get; set; }
    public string UppdateInterval { get; set; }       
    public List<FeedItem> Items { get; set; }
}

This is the Whole Code that I'm trying to get working, filling a ListView with the Title, based on the Name of the Listview with Feed.Name that I select.

private void listFlow_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        listInfo.Items.Clear();
        listEpisode.Items.Clear();
        if (listFlow.SelectedItem != null)
        {

            string flow = listFlow.SelectedItem.ToString();
            var loadFeedData = fillFeed.GetAllFeeds();
            var filteredOrders = loadFeedData
.Where(f => f.Name == myStringFromComboBox)
.SelectMany(f => f.Items)
.Select(fi => fi.Title);

            listEpisode.Items.Add(filteredOrders);

        }
    }

- Posted whole code to clear out some ??

Upvotes: 5

Views: 5744

Answers (3)

Asad Saeeduddin
Asad Saeeduddin

Reputation: 46628

loadFeedData
    .Where(f => f.Name == myStringFromComboBox)
    .SelectMany(f => f.Items)
    .Select(fi => fi.Title);

Upvotes: 9

Habib
Habib

Reputation: 223187

I believe you are looking for:

List<string> titles = loadFeedData.Where(f => f.Name == "SomeName")
                    .SelectMany(f => f.Items.Select(subItem => subItem.Title))
                    .ToList();
  • First you will filter your main list loadFeedData based on Name
  • Then select Title from List<FeedItem>
  • Later flatten your Titles, using SelectMany to return an IEnumerable<string>
  • Optional, call ToList to get a List<string> back.

Upvotes: 2

Christian
Christian

Reputation: 4375

If I understand you correctly, you want to use this one:

var filteredOrders = loadFeedData.Where(x => x.Name == flow)
                                 .SelectMany(x => x.Items)
                                 .Select(x => x.Title).ToList();

This will give you all FeedItem items inside all Feed things, which have Feed.Name == flow.

Upvotes: 0

Related Questions