Solved

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

Posted on 2012-04-12
2
368 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

832 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