Click here to Skip to main content
15,885,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys i'm wanting a simple and quick way (without having to use two foreach loops) to retrieve objects that match my criteria:

I have a IEnumerable<newsitem> currentNews;

a NewsItem object has a property of IEnumberable<subscribers>;
inside the Subscribers objects there is a property of Id;

i would like to retrieve all the NewsItem objects in currentNews where the NewsItem.Subscribers' id = 5

so essentially NewsItem.Subscribers.Id = 5 (but i know this is not correct syntax).

I also am only wanting the first 5 objects in the currentNews, code at the moment (without the subscribers criteria below).

C#
IEnumerable<NewsItem> currentLanguageNews = newsItemCollection._newsItems.Where(n => n.Language == language);
            int i = 0;
            List<INewsItem> latestNews = new List<INewsItem>();
            foreach (INewsItem newsItem in currentLanguageNews)
            {
                if (i < 5)
                {
                    i++;
                    latestNews.Add(newsItem);
                }
                else
                {
                    break;
                }
            }
Posted

1 solution

So you want to take the top 5 news items for the current language, and then filter out any where subscriber 5 isn't subscribed?
C#
IEnumerable<NewsItem> result = newsItemCollection._newsItems
    // Filter by language:
    .Where(n => n.Language == language) 
    // Take the first 5 items:
    .Take(5) 
    // Only items where subscriber 5 is subscribed:
    .Where(n => n.Subscribers.Any(s => s.Id == 5))
;

If you want to take the top 5 items for the subscriber, move the Take call after the second Where call.
 
Share this answer
 
v2
Comments
Grant Weatherston 6-Oct-14 4:29am    
I thought that could be the case, but i didnt know if the last park with the .Any would work as didnt know how it would collect the data, or if it would muddle up the results,with parts that match first bit and then any results in the whole collection tha match the subscribers part.

Or does it work like multiple if's e.g if(i<5).., (if langauge == 'en') move on , foreach(subscriber) if subscriber id = 5 , add to the returned list? and stop at 5 ? is that how the LINQ logic works behind scenes?
Richard Deeming 6-Oct-14 7:04am    
Each LINQ call operates independently, and builds on the previous calls to generate the final list.

The first Where call returns a filtered version of the source list, containing only the items whose language matches.

The Take call returns only the first five items of that filtered list.

The second Where call filters those five items to only return those which contain at least one subscriber with an Id of 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900