Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ??

like image 967
DangerCoder Avatar asked Dec 20 '22 09:12

DangerCoder


2 Answers

loadFeedData
    .Where(f => f.Name == myStringFromComboBox)
    .SelectMany(f => f.Items)
    .Select(fi => fi.Title);
like image 50
Asad Saeeduddin Avatar answered Jan 11 '23 23:01

Asad Saeeduddin


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.
like image 25
Habib Avatar answered Jan 12 '23 00:01

Habib