Solved

Define an Extension Method for IEnumerable<T> which returns IList<T>

Posted on 2012-04-12
2
362 Views
Last Modified: 2012-04-16
Hi experts

Im trying to better understand linq and I was trying to have an Extension Method that returns a IList<T> instead of IEnumerable<T>

I have an Extension Method for IEnumerable<T> which returns IEnumerable<T>

static IEnumerable<T> Where<T>(this IEnumerable<T> data, Func<T, bool> predicate)
{
    foreach(T value in data)
    {
        if(predicate(value)) yield return value;
    }
}

Open in new window


if I use this, I will get this error of "ïs not an iterator interface"

static IList<T> Where<T>(this IEnumerable<T> data, Func<T, bool> predicate)
        {
            //Error	1	The body of 'ConsoleApplication1.EnumerableExtensions.Where<T>
            //(System.Collections.Generic.IEnumerable<T>, System.Func<T,bool>)' 
            //cannot be an iterator block because 'System.Collections.Generic.IList<T>' 
            //is not an iterator interface 

            foreach (T value in data)
            {
                if (predicate(value)) yield return value;
            }
        }

Open in new window


I think that the way to do it is like this

static IList<T> Where<T>(this IEnumerable<T> data, Func<T, bool> predicate)
        {
            return Enumerable.Where(data, predicate).ToList();
        }

Open in new window


Is it valid for all cases? I will like to undertand it better

Thanks
0
Comment
Question by:JoseHidalgo
2 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 37842560
You get that error because you are using the yield return construct, which mandates the method to return either IEnumerable or IEnumerable<T>.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37842668
I don't understand, as LINQ already provides the ToList extension method for IEnumerable.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQl help with selection 14 44
help with issues with ReportViewer in VS2015 5 23
Spacing between controls 4 16
C# TextBox 11 12
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now