Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

896 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

17 Experts available now in Live!

Get 1:1 Help Now